题目传送门 样例能过,是输出格式的问题吗? Code
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int sx, sy, ex, ey;
const int N = 210;
struct node{
int x, y;
};
int r, c;
char mg[N][N];
bool vist[N][N];
int dist[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void bfs() {
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
vist[i][j] = false;
dist[i][j] = 0;
}
}
queue<node> q;
q.push({sx, sy});
while (!q.empty()) {
node f = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nx = f.x + dx[d];
int ny = f.y + dy[d];
if (nx >= 0 && nx < r && ny >= 0
&& ny < c && !vist[nx][ny] && mg[nx][ny] != '#') {
q.push({nx, ny});
vist[nx][ny] = true;
dist[nx][ny] = dist[f.x][f.y] + 1;
}
}
}
}
int main() {
int n;
cin >> n;
while (n--) {
cin >> r >> c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> mg[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (mg[i][j] == 'S') sx = i, sy = j;
if (mg[i][j] == 'E') ex = i, ey = j;
}
}
bfs();
if (dist[ex][ey]) cout << dist[ex][ey] << endl;
else cout << "oop!";
}
return 0;
}