模板
2024-10-20 13:11:32
发布于:云南
0阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y;
};
int n,m,fx,fy;
char mp[45][45];
int dir[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
bool vis[45][45];
bool check(int x,int y){
return x >= 1 && y >= 1 && x <= n && y <= m && vis[x][y] == false && mp[x][y] == '.';
}
void bfs(int x,int y){
queue<node> q;
q.push({x,y});
vis[x][y] = true;
while(!q.empty()){
node f = q.front();
q.pop();
if(f.x == fx && f.y == fy){
cout << "YES";
return;
}
for(int i = 0;i < 4;i++){
int nx = f.x + dir[i][0],ny = f.y + dir[i][1];
if(check(nx,ny)){
vis[nx][ny] = true;
q.push({nx,ny});
}
}
}
cout << "NO";
}
int main(){
cin >> n >> m;
int sx,sy; cin >> sx >> sy;
cin >> fx >> fy;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++) cin >> mp[i][j];
}
bfs(sx,sy);
return 0;
}
这里空空如也
有帮助,赞一个