0326重写,一个小时终于成功了
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int t;
int r,c;
char mp[210][210];
bool vis[210][210];
struct node{
int x,y;
};
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int sx,sy;
int path[210][210] = {-1};
bool bfs(int xx,int yy){
queue<node> q;
q.push({xx,yy});
vis[xx][yy] = true;
path[xx][yy] = 0;
while(!q.empty()){
node temp = q.front();
q.pop();
for(int i=0;i<4;i++){
int nx = dx[i] + temp.x;
int ny = dy[i] + temp.y;
if(mp[nx][ny] == 'E'){
数据更新时要看清变量nx和ny!!!!path[nx][ny] = path[temp.x][temp.y] + 1;!!!
printf("%d\n",path[nx][ny]);
return true;
}
if(path[nx][ny] == -1 && mp[nx][ny] == '#' || nx < 0 || nx >= r || ny < 0 || ny >= c){
continue;
}
if(!vis[nx][ny] && mp[nx][ny] == '.'){
path[nx][ny] = path[temp.x][temp.y] + 1;
vis[nx][ny] = true;
q.push({nx,ny});
}
}
}
return false;
}
int main()
{
scanf("%d",&t);
char s[210];
while(t--){
memset(mp,'#',sizeof(mp));
memset(vis,false,sizeof (vis));
memset(path,-1,sizeof (path));
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++){
scanf("%s",&s);
for(int j=0;j<c;j++){
mp[i][j] = s[j];
if(mp[i][j] == 'S'){
sx = i;
sy = j;
}
}
}
bool ans = bfs(sx,sy);
if(!ans) printf("oop!\n");
}
return 0;
}