hdu_1728_逃离迷宫(bfs)

时间:2022-10-05 08:59:02

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1728

题意:走迷宫,找最小的拐角

题解:对BFS有了新的理解,DFS+剪枝应该也能过,用BFS就要以拐角作为增量来搜,即以当前点为坐标,4个方向都搜一次,下一次出队,step就要加1

 #include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define FFC(i,a,b) for(int i=a;i<=b;i++)
int t,m,n,xs,xe,ys,ye,k,dir[][]={{,},{-,},{,},{,-}};
struct dt{int x,y,t;};
char g[][];bool v[][];
bool check(int x,int y){
if(x>n||x<||y>m||y<||g[x][y]=='*')return ;
return ;
}
bool fuck(){
memset(v,,sizeof(v));
dt st,o;st.x=xs,st.y=ys,st.t=-;
v[xs][ys]=;
queue<dt>Q;Q.push(st);
while(!Q.empty()){
o=Q.front();Q.pop();
if(o.x==xe&&o.y==ye&&o.t<=k)return ;
o.t++;
for(int i=;i<;i++){
int xx=o.x+dir[i][],yy=o.y+dir[i][];
while(check(xx,yy)){
if(!v[xx][yy]){st.x=xx,st.y=yy,st.t=o.t,v[xx][yy]=;Q.push(st);}
xx+=dir[i][],yy+=dir[i][];
}
}
}
return ;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
FFC(i,,n){
getchar();
FFC(j,,m)scanf("%c",&g[i][j]);
}
scanf("%d%d%d%d%d",&k,&ys,&xs,&ye,&xe);
if(fuck())puts("yes");
else puts("no"); }
return ;
}