(BFS)poj2935-Basic Wall Maze

题目地址

题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走。另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标。这样回溯(方法十分经典)就可以顺利的输出。

这道题难度的确很小,可是我却花了近两个小时才顺利AC,实在是现在水平太不足了,要努力学习的真的是有好多啊。不管怎样,尽力吧。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<queue>
  5 using namespace std;
  6 struct grid
  7 {
  8     int x,y;//坐标
  9     int pre;//回溯前一个的下标
 10     int dir;
 11 }point[5025];
 12 int si,sj,ei,ej,a[4],dir[10][10][4],direction[4][2]={{-1,0},{0,1},{1,0},{0,-1}},vi[8][8];
 13 int  bfs()
 14 {
 15     memset(vi,0,sizeof(vi));
 16     int front=0,tail=1;
 17     point[front].x=si;
 18     point[front].y=sj;
 19     point[front].pre=-1;
 20     grid temp,temp2;
 21     vi[si][sj]=1;
 22     while(front<tail)
 23     {
 24         temp=point[front];
 25         for(int i=0;i<4;i++)
 26         {
 27             if(dir[temp.x][temp.y][i]==1){continue;}
 28             else
 29             {
 30                 temp2.x=temp.x+direction[i][0];
 31                 temp2.y=temp.y+direction[i][1];
 32                 temp2.dir=i;
 33                 if(vi[temp2.x][temp2.y])
 34                     continue;
 35                 else
 36                 {
 37                     temp2.pre=front;
 38                     vi[temp2.x][temp2.y]=1;
 39                     point[tail++]=temp2;
 40                     if(temp2.x==ei&&temp2.y==ej)
 41                         return (tail-1);
 42                 }
 43             }
 44         }
 45         front++;
 46     }
 47 }
 48 void print(int x)//经典的输出方式
 49 {
 50     if(x>0)
 51         {
 52         print(point[x].pre);
 53         if(point[x].dir==0)
 54         {
 55             printf("N");
 56         }
 57         else if(point[x].dir==1)
 58         {
 59             printf("E");
 60         }
 61         else if(point[x].dir==2)
 62         {
 63             printf("S");
 64         }
 65         else printf("W");
 66         }
 67 
 68 }
 69 int main()
 70 {
 71     while(scanf("%d%d",&sj,&si))
 72     {
 73         int i,j,k,an;
 74         if(si==0&&sj==0)
 75             break;
 76         scanf("%d%d",&ej,&ei);
 77         memset(dir,0,sizeof(dir));
 78         for(i=1;i<=6;i++)
 79         {
 80             dir[1][i][0]=1;
 81             dir[6][i][2]=1;
 82             dir[i][1][3]=1;
 83             dir[i][6][1]=1;
 84         }
 85         for(j=0;j<3;j++){
 86         scanf("%d %d %d %d",&a[1],&a[0],&a[3],&a[2]);//注意数据是行列与平常不同的
 87 
 88         if(a[0]==a[2])
 89         {
 90             for(k=min(a[1],a[3])+1;k<max(a[1],a[3])+1;k++)
 91             {
 92                 dir[a[0]+1][k][0]=1;
 93                 dir[a[0]][k][2]=1;
 94             }
 95         }
 96         else
 97         {
 98             for(k=min(a[0],a[2])+1;k<max(a[0],a[2])+1;k++)
 99             {
100                 dir[k][a[1]+1][3]=1;
101                 dir[k][a[1]][1]=1;
102             }
103         }
104     }
105     an=bfs();
106     print(an);
107     puts("");
108     }
109     return 0;
110 }
原文地址:https://www.cnblogs.com/quintessence/p/6036668.html