怀疑是输出路线错了,但依旧调不出来。求调啊……(悬1关)
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct Node{int x, y, num;};
queue<Node> q;
char tp[2001][2001];
int a[2001][2001], ans1[2001][2001], n, m;
char ch[4] = {'R', 'L', 'D', 'U'};
string ans2;
int wk[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
void print(int ux, int uy)
{
for (int i = 0; i < 4; i++)
{
int x = ux + wk[i][0], y = uy + wk[i][1];
if (x < 0 || y < 0 || x > n || y > m || a[x][y] != a[ux][uy] + 1 || ans1[x][y] == -1) continue;
cout << ch[i];
print(x, y);
}
}
void solve()
{
memset(ans1, -1, sizeof(ans1));
ans2 = "";
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
cin >> tp[i][j];
a[i][j] = tp[i][j] - '0';
}
q.push((Node){0, 0, a[0][0]});
ans1[0][0] = 0;
while (!q.empty())
{
Node u = q.front();
int ux = u.x, uy = u.y;
q.pop();
for (int i = 0; i < 4; i++)
{
int x = ux + wk[i][0], y = uy + wk[i][1];
if (x < 0 || y < 0 || x > n || y > m || a[x][y] == a[ux][uy] || ans1[x][y] != -1) continue;
ans1[x][y] = ans1[ux][uy] + 1;
q.push((Node){x, y});
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << ans1[i][j] << ' ';
cout << endl;
}
if (ans1[n-1][m-1] != -1)
{
cout << ans1[n-1][m-1] << endl;
print(0, 0);
cout << endl;
}
else cout << -1 << endl;
}
/*
5 5
00010
10111
10110
00101
01010
*/
int main()
{
int T;
cin >> T;
while (T--) solve();
}