想了n天的一直不AC,求大佬看
查看原帖
想了n天的一直不AC,求大佬看
448910
_Goodnight楼主2021/9/1 22:51
毕竟还是他人最容易发现自己的错误嘛,求求大佬了,我已经照着题解都打了不下于5遍了,一直有过不去的数据
#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;

struct p {
	int x, y, t;
};
queue<p>Q;
char a[400][400];
int b[400][400];
int n, m, sx, sy, nex[4][2]{ {0,1},{1,0},{0,-1},{-1,0} };
void transport(int& x, int& y) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (a[i][j] == a[x][y] && i != x && j != y) {
				x = i, y = j;
				return;
			}
		}
	}
}
int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] == '@')
				sx = i, sy = j;
		}
	}
	Q.push({ sx,sy,0 });
	while (!Q.empty()) {
		p u = Q.front();
		Q.pop();
		if (a[u.x][u.y] == '=') {
			cout << u.t;
			return 0;
		}
		if (a[u.x][u.y] >= 'A' && a[u.x][u.y] <= 'Z') {
			transport(u.x, u.y);
		}
		for (int k = 0; k < 4; k++) {
			int tx = u.x + nex[k][0], ty = u.y + nex[k][1];
			if (tx >= 0 && tx < n && ty >= 0 && ty < m && a[tx][ty] != '#' && b[tx][ty] == 0) {
				Q.push({ tx,ty,u.t + 1 });
				b[tx][ty] = 1;
			}
		}
	}
}
2021/9/1 22:51
加载中...