真的不知道哪错,求大佬帮帮
查看原帖
真的不知道哪错,求大佬帮帮
1308662
Tiks_code楼主2024/3/17 11:25
#include <bits/stdc++.h>

using namespace std;

const int N = 1e2;

pair<int, int >st, ed;

int n;

int vis[N][N] = { 0 };

int a[N][N] = { 0 };

struct node {
	int x, y, cnt;
};

queue<node>q;

int dy[] = { -1,0,1,0 };
int dx[] = { 0,1,0,-1 };

int bfs(int x, int y, int cnt) {
	q.push(node{ x,y,cnt });
	vis[x][y] = 1;
	while (!q.empty()) {
		auto u = q.front();
		if (ed.first == u.x && ed.second == u.y) return u.cnt;
		q.pop();
		int x = u.x, y = u.y;
		for (int i = 0; i < 4; i++) {
			int tx = x + dx[i];
			int ty = y + dy[i];
			if (tx < 0 || tx > n || ty < 0 || ty > n) continue;
			if (vis[tx][ty] || a[tx][ty]) continue;

			vis[tx][ty] = 1;
			q.push(node{ tx ,ty ,cnt + 1 });
		}
	}
	return -1;
}

void sub(int x, int i) {
	while (x) {
		for (int j = 1; j <= n; j++) {
			string str = to_string(x);
			str.reserve();
			int temp = stoi(str);
			a[i][j] = temp % 10;
			temp /= 10;
			x /= 10;
		}
	}
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		sub(x, i);
	}
	cin >> st.first >> st.second >> ed.first >> ed.second;
	cout << bfs(st.first, st.second, 0) << endl;
	system("pause");
	return 0;
}

真的不知道哪错,求大佬帮帮

2024/3/17 11:25
加载中...