为啥同一个值两次输出不同啊
查看原帖
为啥同一个值两次输出不同啊
643781
it_hard_to_name楼主2022/8/4 19:56
/*
输入样例
5 6
###=##
#.W.##
#.####
#.@W##
######
*/
#include <bits/stdc++.h>
using namespace std;

struct p {
	int x, y;
} q[2000], mov[305][305];
int n, m, head, tail = 1;
char a[305][305];
bool vis[305][305];

int d[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}, t[305][305];
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			if (a[i][j] == '@')
				q[tail].x = i, q[tail].y = j;
			if (a[i][j] <= 'Z' && a[i][j] >= 'A') {//如果是传送门
				int dx = mov[a[i][j]][0].x;
				int dy = mov[a[i][j]][0].y;
				if (dx) {//第二次 记录传送门传送坐标
					mov[i][j] = {dx, dy};
					mov[dx][dy] = {i, j};
				} else {//第一次遇到
					mov[a[i][j]][0] = {i, j};//记录此大写字母位置
				}
			}
		}
	}
	vis[q[tail].x][q[tail].y] = 1;
	while (head < tail) {
		head++;
		struct p h = q[head];
		if (a[h.x][h.y] == '=') {
			cout << t[h.x][h.y];
			return 0;
		}
		if (a[h.x][h.y] <= 'Z' && a[h.x][h.y] >= 'A') {
			cout << mov[h.x][h.y].x << ' ' << mov[h.x][h.y].y << endl;   //输出 2,3;4,4
			h.x = mov[h.x][h.y].x;
			h.y = mov[h.x][h.y].y;
			cout << h.x << ' ' << h.y << endl;   //输出2,0;4,0;
		}
		for (int i = 0; i < 4; i++) {
			int dx = d[i][0] + h.x;
			int dy = d[i][1] + h.y;
			if (dx > n || dx < 1 || dy < 1 || dy > m || a[dx][dy] == '#' || vis[dx][dy])
				continue;
			t[dx][dy] = t[h.x][h.y] + 1;
			vis[dx][dy] = 1;
			struct p c = {dx, dy};
			q[++tail] = c;
		}
	}
	return 0;
}
2022/8/4 19:56
加载中...