HDU 1728 逃离迷宫

这题真尼玛坑、 后面那个输入起点和终点的 先输入列在输入行、

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstdio>
 7 using namespace std;
 8 int n,m,k;
 9 int sx,sy,tx,ty;
10 int t;
11 const int qq=110;
12 char map[qq][qq];
13 int vis[qq][qq];
14 int dir[4][2]={1,0,-1,0,0,1,0,-1}; //下 上 右 左、  
15 struct Node                                
16 {
17     int x,y;
18     int count;
19 };
20 int check(int x,int y)
21 {
22     if(x>=1&&x<=n&&y>=1&&y<=m&&map[x][y]=='.')        //条件允许时返回真、 
23         return 1;
24     return 0;
25 }
26 void bfs()
27 {    
28     queue<Node>Q;
29     Node ans,cns;
30     ans.x=sx;ans.y=sy;ans.count=-1;
31     vis[ans.x][ans.y]=1;
32     Q.push(ans);
33     t=0;
34     int flag=0;
35     while(!Q.empty()){
36         cns=Q.front();
37         Q.pop();
38         //printf("%d %d %d
",cns.x,cns.y,cns.count);
39         if(cns.x==tx&&cns.y==ty&&cns.count<=k)
40             flag=1;
41         if(flag)    break;
42         ans.count=cns.count+1;
43         for(int i=0;i<4;++i){
44             ans.x=cns.x+dir[i][0];
45             ans.y=cns.y+dir[i][1];
46             while(check(ans.x,ans.y)){
47                 if(!vis[ans.x][ans.y]){        //这里是最重要的一点只有保证了这一点才保证了BFS贪心的思路、 
48                     Q.push(ans);            // 也就是说这点必须放在这里判断、不能再check函数里面 
49                     vis[ans.x][ans.y]=1;    
50                 }
51                 ans.x+=dir[i][0];
52                 ans.y+=dir[i][1];
53             }
54         }
55     }
56     if(flag)    printf("yes
");
57     else        printf("no
");
58     while(!Q.empty())
59         Q.pop();
60     return;
61 }
62 int main()
63 {
64     int t;
65     scanf("%d",&t);
66     while(t--){
67         memset(vis,0,sizeof(vis));
68         scanf("%d %d",&n,&m);
69         for(int i=1;i<=n;++i)
70                 scanf("%s",map[i]+1);
71         //for(int i=1;i<=n;++i){
72         //    for(int j=1;j<=m;++j)
73         //        printf("%c",map[i][j]);
74         //        printf("
");
75         //}
76         scanf("%d %d %d %d %d",&k,&sy,&sx,&ty,&tx);
77     //    printf("%d
",k);
78         bfs();
79     }
80     return 0;
81 }
原文地址:https://www.cnblogs.com/sasuke-/p/5262669.html