HDU1728--逃离迷宫

来源:互联网 发布:都昌最美村庄网络评选 编辑:程序博客网 时间:2024/05/14 15:51

/**

*简单的BFS

*/


#include<stdio.h>#include<queue>using namespace std;char map[110][110];int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int flag[110][110];//记录x,y坐标节点当前所用的最小的转弯次数。int m,n,sx,sy,ex,ey,MAXValue;struct Node{/*** x,y当前所在的坐标位置* dir方向用 0,1,2,3表示四个方向* step转弯的次数*/    int x,y,dir,step;};bool check(int x,int y){    if(x>=0 && x<m && y>=0 && y<n)return true;    return false;}int search(){    int i;    queue<Node>Q;    Node t,temp;    t.x=sx;    t.y=sy;    t.step=-1;    t.dir=-1;    flag[t.x][t.y]=1;    Q.push(t);    while(!Q.empty()){        t=Q.front();        Q.pop();        //在转弯数允许的范围内从起点到达终点        if(t.x==ex && t.y==ey && t.step<=MAXValue)return 1;        for(i=0;i<4;i++){            int tx=dir[i][0]+t.x;            int ty=dir[i][1]+t.y;            //搜索下一节点是否可走            if(check(tx,ty) && flag[tx][ty]>=t.step && t.step<=MAXValue && map[tx][ty]!='*'){                temp.x=tx;                temp.y=ty;                //当前节点方向与上一节点方向不同,则转弯次数加1                if(t.dir!=i){                    temp.step=t.step+1;                }else{                //方向相同,转弯数不变                    temp.step=t.step;                }                //设置当前节点的方向                temp.dir=i;                //记录搜索到当前节点时最小的转弯次数                if(temp.step<=flag[tx][ty])                    flag[tx][ty]=temp.step;                Q.push(temp);            }        }    }    return 0;}int main(){    int t,i,j;    scanf("%d",&t);    while(t--){        scanf("%d%d",&m,&n);        for(i=0;i<110;i++){            for(j=0;j<110;j++)flag[i][j]=99999999;        }        for(i=0;i<m;i++){            scanf("%s",map[i]);        }        scanf("%d%d%d%d%d",&MAXValue,&sy,&sx,&ey,&ex);        sy--;        sx--;        ey--;        ex--;        int result=search();        if(result)printf("yes\n");        else printf("no\n");    }}


原创粉丝点击