[BFS] Zjut CPP 1907 破解迷宫

来源:互联网 发布:腾讯大数据 编辑:程序博客网 时间:2024/06/05 05:43

简单BFS 


#include <iostream>#include <queue>using namespace std;int m,n;char str[105][105];int ps[4][2]={1,0,0,-1,-1,0,0,1};struct Node{int x;int y;};Node s,e,r;queue <Node> qu;int main(){int t;cin>>t;for (;t>=1;t--){cin>>n>>m;bool found=false;for (int i=0;i<n;i++)for (int j=0;j<m;j++){cin>>str[i][j];if (str[i][j]=='M')str[i][j]='X';else if (str[i][j]=='S')s.x=i,s.y=j;else if (str[i][j]=='T')e.x=i,e.y=j;}qu.push(s);while (!qu.empty() && !found){s=qu.front();qu.pop();for (int p=0;p<4;p++){r.x=s.x+ps[p][0];r.y=s.y+ps[p][1];if (r.x>=0 && r.y>=0 && r.x<n && r.y<m && str[r.x][r.y]!='X'){if (str[r.x][r.y]=='T'){found=true;break;}str[r.x][r.y]='X';qu.push(r);}}}if (found)cout<<"YES\n";else cout<<"NO\n";}return 0;}

0 0
原创粉丝点击