uva 10047 The Monocycle(搜索)

好复杂的样子。。其实就是纸老虎,多了方向、颜色两个状态罢了,依旧是bfs。

更新的时候注意处理好就行了,vis[][][][]要勇敢地开。

不过这个代码交了十几遍的submission error,手都软了

代码应该是没有问题,也可以参考http://blog.csdn.net/wjjayo/article/details/5809911

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int MAXN=30;
 7 
 8 struct P{
 9     int x,y,d,c;
10     int bfn;
11     P(){}
12     P(int _x,int _y,int _d,int _c,int _bfn):x(_x),y(_y),d(_d),c(_c),bfn(_bfn){}
13 }que[MAXN*MAXN*20];
14 
15 char mp[MAXN][MAXN],n,m;
16 int vis[MAXN][MAXN][10][10],tot;
17 int dis[4][2]={-1,0,0,-1,1,0,0,1};
18 int ex,ey;
19 
20 void bfs(int a,int b)
21 {
22     int l,r;
23     l=r=0;
24     que[r].x=a;
25     que[r].y=b;
26     que[r].d=0;
27     que[r].c=0;
28     que[r++].bfn=0;
29     vis[a][b][0][0]=1;
30     while(l<r)
31     {
32         int x=que[l].x;
33         int y=que[l].y;
34         int d=que[l].d;
35         int c=que[l].c;
36         int bfn=que[l++].bfn;
37 
38         if(x==ex&&y==ey&&c==0)
39             break;
40 
41         int dx=dis[d][0];
42         int dy=dis[d][1];
43         if(!vis[x+dx][y+dy][d][(c+1)%5]&&mp[x+dx][y+dy]!='#'&&x+dx>=0&&x+dx<n&&y+dy>=0&&y+dy<m){
44             que[r++]=P(x+dx,y+dy,d,(c+1)%5,bfn+1);
45             vis[x+dx][y+dy][d][(c+1)%5]=1;
46         }
47         if(!vis[x][y][(d+4-1)%4][c]){
48             que[r++]=P(x,y,(d+4-1)%4,c,bfn+1);
49             vis[x][y][(d+4-1)%4][c]=1;
50         }
51         if(!vis[x][y][(d+1)%4][c]){
52             que[r++]=P(x,y,(d+1)%4,c,bfn+1);
53             vis[x][y][(d+1)%4][c]=1;
54         }
55     }
56     if(l<r)
57         printf("Case #%d
minimum time = %d sec
",++tot,que[l-1].bfn);
58     else
59         printf("Case #%d
destination not reachable
",++tot);
60 }
61 
62 int main()
63 {
64     int sx,sy;
65     int i,j;
66     tot=0;
67     while(scanf("%d%d",&n,&m)!=EOF)
68     {
69 
70         if(tot)
71             printf("
");
72         if(!n&&!m)
73             return 0;
74         for(i=0;i<n;i++)
75         {
76             getchar();
77             for(j=0;j<m;j++)
78             {
79                 scanf("%c",&mp[i][j]);
80                 if(mp[i][j]=='S'){
81                     sx=i;sy=j;
82                 }
83                 if(mp[i][j]=='T'){
84                     ex=i;ey=j;
85                 }
86             }
87         }
88         bfs(sx,sy);
89     }
90     return 0;
91 }
View Code
原文地址:https://www.cnblogs.com/zstu-abc/p/3228460.html