T326482 寻找『TA』题解
由题目可知『呼吸然』这个很菜而且还贪婪的路痴想找到『TA』,让我们来求最短路径。
一看到求最短路径的地图题,我们很容易就能想到用广度优先搜索来解决这个问题,于是我们随便写一个广搜就可以轻松的AC。
AC code:
#include <bits/stdc++.h>
using namespace std;
int nxt_x[4]={0,0,-1,1};
int nxt_y[4]={-1,1,0,0};
int n,m,sx,sy,ex,ey,ans=-1;
bool tu[8010][8010];
struct node{
int x,y,dep;
};
void bfs(){
queue<node> q;
q.push(node{sx,sy,0});
while(q.size()){
node t=q.front();
q.pop();
for(int i=0;i<4;i++){
int nx=t.x+nxt_x[i],ny=t.y+nxt_y[i],ndep=t.dep+1;
if(nx==ex&&ny==ey){
ans=ndep;
return;
}
if(!tu[ny][nx]&&nx>=1&&nx<=m&&ny>=1&&ny<=n){
q.push(node{nx,ny,ndep});
tu[ny][nx]=1;
}
}
}
}
int main(){
scanf("%d%d\n",&n,&m);
char c;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c;
if(c=='!') tu[i][j]=1;
if(c=='H') sx=j,sy=i;
if(c=='T') ex=j,ey=i;
}
}
bfs();
printf("%d",ans);
return 0;
}
最后,我们来一起快乐的帮助『呼吸然』出题吧

浙公网安备 33010602011771号