求助大佬 40 pts 其余全 RE
查看原帖
求助大佬 40 pts 其余全 RE
250291
yuchenren楼主2021/10/7 16:09

蒟蒻太蒻了,求帮忙找 bug(已经看到眼花了)

#include <bits/stdc++.h>
using namespace std;

struct NODE {
	int x, y;
};

queue <NODE> q;

int n, x, y, nextx, nexty, dis[1001][1001], instartx, instarty, tarx, tary, dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
char MAP[1001][1001];
bool vis[1001][1001];

int bfs(int startx, int starty) {
	q.push((NODE){startx, starty});
	vis[startx][starty] = true;
	while (!q.empty()) {
		x = q.front().x;
		y = q.front().y;
		q.pop();
		if (x == tarx && y == tary) {
			return dis[x][y];
		}
		for (int i = 0; i < 4; i++) {
			nextx = x + dx[i];
			nexty = y + dy[i];
			if ((MAP[nextx][nexty] == '1' || vis[nextx][nexty] == true) || (nextx <= 0 || nextx > n || nexty <= 0 || nexty > n)) {
				continue;
			}
			dis[nextx][nexty] = dis[x][y] + 1;
			vis[nextx][nexty] = true;
			q.push((NODE){nextx, nexty});
		}
	}
	return -1;
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> MAP[i][j];
		}
	}
	cin >> instartx >> instarty >> tarx >> tary;
	cout << bfs(instartx, instarty) << endl;
	return 0;
}
2021/10/7 16:09
加载中...