9178
2025-07-30 20:03:35
发布于:广东
#include <iostream>
#include <queue>
using namespace std;
const int N = 50;
struct node{
int x;
int y;
int d;
};
char mp[N][N];
bool vis[N][N];
int dir[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };// 上下左右
int r, c,ans=0;
int x1 , y1 , x2 , y2; //起点终点坐标
int BFS(int x, int y){
queue<node> q;
q.push({x, y, 0});
vis[x][y] = true;
while(!q.empty()){
node t = q.front();
q.pop();
if(mp[t.x][t.y]=='E') ans++;
for(int i = 0; i < 4; i++){
int nx = t.x + dir[i][0];
int ny = t.y + dir[i][1];
if(nx >= 1 && nx <= r && ny >= 1 && ny <= c && !vis[nx][ny] && mp[nx][ny] != '#'){
q.push({nx, ny, t.d + 1});
vis[nx][ny] = true;
}
}
}
return -1;
}
int main(){
freopen("trap.in", "r", stdin);
freopen("trap.out","w",stdout);
cin >> r >> c;
for(int i = 1; i <= r; i++){
for(int j = 1; j <= c; j++){
cin >> mp[i][j];
}
}
BFS(1,1);
cout<<ans;
fclose(stdin);
fclose(stdout);
return 0;
}
这里空空如也
有帮助,赞一个