为什么!!!!!!!!!!
查看原帖
为什么!!!!!!!!!!
992134
weiyi8910楼主2025/1/14 08:24
#include <bits/stdc++.h>
using namespace std;
int aaaaaa[1000][1000], n, m, t;



int xxx[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int bbb[1000][1000];

struct weiyi {
	int x, y;
};
queue<weiyi>q;

void bfs() {

	while (!q.empty()) {
		int ux = q.front().x, uy = q.front().y;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int d = bbb[ux][uy];
			int vx = ux + xxx[i][0], vy = uy + xxx[i][1];
			if (!vx || !vy || vx > n || vy > m || bbb[vx][vy] >= 0 || aaaaaa[ux][uy] == aaaaaa[vx][vy]) {

			} else {

				bbb[vx][vy] = d + 1;
				q.push({vx, vy});
			}
		}
	}
}

void dfs(int x, int y) {

	if (x == 1 && y == 1) {
		return ;
	}  else {
		if (bbb[x - 1][y] == bbb[x][y] - 1) {
			dfs(x - 1, y);
			cout << 'D';
		} else if (bbb[x][y - 1] == bbb[x][y] - 1) {
			dfs(x, y - 1);
			cout << 'R';
		} else if (bbb[x + 1][y] == bbb[x][y] - 1) {
			dfs(x + 1, y);
			cout << 'U';
		} else if (bbb[x][y + 1] == bbb[x][y] - 1) {
			dfs(x, y + 1);
			cout << "L";
		}

	}
}

int main() {
	cin >> t;
	while (t--) {
		memset(bbb, -1, sizeof(bbb));
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				char c;
				cin >> c;
				aaaaaa[i][j] = c == '1';
			}
		}
		q.push({1, 1});
		bbb[1][1] =  0;
		bfs();
		cout << bbb[n][m] << '\n';
		dfs(n, m);

	}
	return 0;
}
/*5
5
01010
01011
01010
11010
01010
*/
2025/1/14 08:24
加载中...