以下是我的程式碼
我發現這樣怎麼輸入測資都會RE,但是只要一把底下的""while(!qi.empty()){"" 整串迴圈刪掉就可以跑了 如果不刪的話連這個迴圈都還跑不到就RE了,請問這是什麼問題呢?
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
int T, R, C, i, j, k;
while(cin >> T){
for(k = 1; k <= T; k ++){
queue<int> qi;
queue<int> qj;
vector<int> Fi;
vector<int> Fj;
bool Escaped = false;
cin >> R >> C;
char maze[R + 2][C + 2];
int D[R + 1][C + 1] = {0};
for(i = 1; i <= R; i ++){
for(j = 1; j <= C; j ++){
cin >> maze[i][j];
if(maze[i][j] == 'J'){
qi.push(i);
qj.push(j);
}
else if(maze[i][j] == 'F'){
Fi.push_back(i);
Fj.push_back(j);
}
}
}
int d;
// BFS
while(!qi.empty()){
// Joe move first
int x = qi.front();
int y = qj.front();
qi.pop();
qj.pop();
if(maze[x][y] != 'F'){
// up down left right
if(maze[x - 1][y] == '.'){
qi.push(x - 1);
qj.push(y);
D[x - 1][y] = D[x][y] + 1;
}
else if(maze[x - 1][y] == '9'){
Escaped = true;
d = D[x][y] + 1;
break;
}
if(maze[x + 1][y] == '.'){
qi.push(x + 1);
qj.push(y);
D[x + 1][y] = D[x][y] + 1;
}
else if(maze[x + 1][y] == '9'){
Escaped = true;
d = D[x][y] + 1;
break;
}
if(maze[x][y - 1] == '.'){
qi.push(x);
qj.push(y - 1);
D[x][y - 1] = D[x][y] + 1;
}
else if(maze[x][y - 1] == '9'){
Escaped = true;
d = D[x][y] + 1;
break;
}
if(maze[x][y + 1] == '.'){
qi.push(x);
qj.push(y + 1);
D[x][y + 1] = D[x][y] + 1;
}
else if(maze[x][y + 1] == '9'){
Escaped = true;
d = D[x][y] + 1;
break;
}
int len = Fi.size();
for(i = 0; i < len; i ++){
bool Stuck = true;
int X = Fi[i];
int Y = Fj[i];
if(maze[X - 1][Y] != '#' && maze[X - 1][Y] != 'F'){
maze[X - 1][Y] = 'F';
Fi.push_back(X - 1);
Fj.push_back(Y);
Stuck = false;
}
if(maze[X + 1][Y] != '#' && maze[X + 1][Y] != 'F'){
maze[X + 1][Y] = 'F';
Fi.push_back(X + 1);
Fj.push_back(Y);
Stuck = false;
}
if(maze[X][Y - 1] != '#' && maze[X][Y - 1] != 'F'){
maze[X][Y - 1] = 'F';
Fi.push_back(X);
Fj.push_back(Y - 1);
Stuck = false;
}
if(maze[X][Y + 1] != '#' && maze[X][Y + 1] != 'F'){
maze[X][Y + 1] = 'F';
Fi.push_back(X);
Fj.push_back(Y + 1);
Stuck = false;
}
if(Stuck){
Fi.erase(Fi.begin() + i);
Fj.erase(Fj.begin() + j);
}
}
x }
}
if(Escaped) cout << d << '\n';
else cout << "IMPOSSIBLE\n";
}
}
}