#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e3 + 5;
char a[maxn][maxn];
bool vis[maxn][maxn];
int T, n, m;
struct node {
int x, y;
int step;
};
const int fx[] = {0, 0, 1, 0, -1};
const int fy[] = {0, 1, 0, -1, 0};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> T;
while (T--) {
cin >> n >> m;
int sx, sy, ex, ey;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == 'r') sx = i, sy = j;
if (a[i][j] == 'a') ex = i, ey = j;
vis[i][j] = 0;
}
}
queue<node> q;
q.push({sx, sy, 0});
vis[sx][sy] = 1;
bool flag = false;
while (q.size() > 0) {
node u = q.front(); q.pop();
for (int i = 1; i <= 4; i++) {
int tx = u.x + fx[i];
int ty = u.y + fy[i];
if (tx <= 0 || tx > n || ty <= 0 || ty > m) continue;
if (a[tx][ty] == '#' || vis[tx][ty]) continue;
if (a[tx][ty] == '@') q.push({tx, ty, u.step + 1});
else if (a[tx][ty] == 'x') q.push({tx, ty, u.step + 2});
else if (tx == ex && ty == ey) {
cout << u.step + 1 << "\n";
flag = true;
break;
}
vis[tx][ty] = true;
}
if (flag == true) break;
}
if (!flag) cout << "Impossible\n";
}
return 0;
}
回复请 AT 我一下谢谢