题目连接: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[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct dt{int x,y,t;};
char g[101][101];bool v[101][101];
bool check(int x,int y){
if(x>n||x<1||y>m||y<1||g[x][y]=='*')return 0;
return 1;
}
bool fuck(){
memset(v,0,sizeof(v));
dt st,o;st.x=xs,st.y=ys,st.t=-1;
v[xs][ys]=1;
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 1;
o.t++;
for(int i=0;i<4;i++){
int xx=o.x+dir[i][0],yy=o.y+dir[i][1];
while(check(xx,yy)){
if(!v[xx][yy]){st.x=xx,st.y=yy,st.t=o.t,v[xx][yy]=1;Q.push(st);}
xx+=dir[i][0],yy+=dir[i][1];
}
}
}
return 0;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
FFC(i,1,n){
getchar();
FFC(j,1,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 0;
}