uestc 一个简单的迷宫问题

我不知道化成网格线什么意思啊。。。 :(

如果我写一步一搜的话肯定很麻烦。。

大牛代码  理解以上两点之后再重新做。。sigh~

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <queue>
  4 #include <string.h>
  5 using namespace std;
  6 int n,m;
  7 int map[55][55];
  8 bool vis[4][55][55];
  9 struct node
 10 {
 11     int x,y;
 12     int dr;   //方向
 13     int step;
 14     bool operator<(const node &t)const
 15     {
 16         return t.step<step;
 17     }
 18 }S,E;
 19 bool judge(int x,int y)
 20 {
 21     if(x>=0 && x<n-1 && y>=0 && y<m-1)
 22     {
 23         return true;
 24     }
 25     return false;
 26 }
 27 int bfs()
 28 {
 29     priority_queue<node>q;
 30     q.push(S);
 31     node now,next,temp;
 32     memset(vis, false, sizeof(vis));
 33     vis[S.dr][S.x][S.y]=true;
 34     while (!q.empty())
 35     {
 36        next=now=temp=q.top();
 37         q.pop();
 38         if(now.x==E.x && now.y==E.y)
 39         {
 40             return now.step;
 41         }
 42         for (int i=-1; i<4; i++)
 43         {
 44             if(i<=0)
 45             {
 46                 now.step=temp.step+1;
 47                 if(i==-1)
 48                 {
 49                     now.dr=(1+temp.dr)%4;  //右转
 50                 }
 51                 if(i==0)
 52                 {
 53                     now.dr=(3+temp.dr)%4;  //左转
 54                 }
 55                 if(!vis[now.dr][now.x][now.y])
 56                 {
 57                     vis[now.dr][now.x][now.y]=true;
 58                     q.push(now);
 59                 }
 60             }
 61             else
 62             {
 63                 next.step=temp.step+1;
 64                 if(next.dr==0)  //向上走
 65                 {
 66                     next.x=temp.x-i;
 67                 }
 68                 if(next.dr==1)  //向右走
 69                 {
 70                     next.y=temp.y+i;
 71                 }
 72                 if(next.dr==2) //向下走
 73                 {
 74                     next.x=temp.x+i;
 75                 }
 76                 if(next.dr==3) //向左走
 77                 {
 78                     next.y=temp.y-i;
 79                 }
 80                 //走i步过程中有一步走不通就不得行了
 81                 if(!judge(next.x, next.y) || map[next.x][next.y]==1)
 82                 {
 83                     break;
 84                 }
 85                 if(!vis[next.dr][next.x][next.y])
 86                 {
 87                     q.push(next);
 88                     vis[next.dr][next.x][next.y]=true;
 89                 }
 90             }
 91         }
 92     }
 93     return -1;
 94 }
 95 int main()
 96 {
 97     while (scanf("%d%d",&n,&m) && (n||m))
 98     {
 99         for (int i=0; i<n; i++)
100         {
101             for (int j=0; j<m; j++)
102             {
103                 scanf("%d",&map[i][j]);
104                 
105                 //把n*m的矩阵化成(n-1)*(m-1)的网格线矩阵。
106                 if(map[i][j]==1)
107                 {
108                     map[i][j]=1;
109                     if(i>=1)
110                     {
111                         map[i-1][j]=1;
112                         if(j>=1)
113                         {
114                             map[i-1][j-1]=1;
115                         }
116                     }
117                     if(j>=1)
118                     {
119                         map[i][j-1]=1;
120                         if(i>=1)
121                         {
122                             map[i-1][j-1]=1;
123                         }
124                     }
125                 }
126             }
127         }
128         scanf("%d%d%d%d%d",&S.x,&S.y,&E.x,&E.y,&S.dr);
129         S.x--,S.y--,E.x--,E.y--;
130         S.step=0;
131         printf("%d\n",bfs());
132     }
133     return 0;
134 }

 

posted @ 2015-01-31 23:39  UsedRose  阅读(150)  评论(0编辑  收藏  举报