Problem A: The Monocycle
题意:题目意思比较绕,就是给出图,从起点'S'出发,到终点'T',有这样的车轮:车轮每转90度,时间加1,上面的均匀5个扇形,从起点开始,是blue色扇形着地,方向向‘上’,然后求出从起点到终点的最后终点是blue扇形着地的最小时间。
题解:用BFS,counts【x】【y】【dir】【co】表示到达x,y时候方向为dir,颜色是co的最小步数,然后就可以BFS。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #define INF 1000000000 7 using namespace std; 8 const int N=30; 9 struct Node{ 10 int x; 11 int y; 12 int step; 13 int dir; 14 int color; 15 }; 16 int n,m; 17 char map[N][N]; 18 int counts[N][N][N][N]; 19 int sx,sy; 20 int ex,ey; 21 void BFS(int x,int y,int d,int color){//0是北1是西2是南3是东 0是绿色,1是白2蓝3红4黑 22 queue<Node>Q; 23 Node temp; 24 temp.x=x; 25 temp.y=y; 26 temp.dir=d; 27 temp.color=color; 28 temp.step=0; 29 for(int i=1;i<=25;i++) 30 for(int j=1;j<=25;j++) 31 for(int k=0;k<=25;k++) 32 for(int g=0;g<=25;g++) 33 counts[i][j][k][g]=INF; 34 Q.push(temp); 35 counts[x][y][d][color]=0; 36 int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; 37 while(!Q.empty()){ 38 Node tt=Q.front(); 39 Q.pop(); 40 int xx=tt.x; 41 int yy=tt.y; 42 int step=tt.step; 43 int dd=tt.dir; 44 int co=tt.color; 45 if(step+1<counts[xx][yy][(dd+3)%4][co]){ 46 counts[xx][yy][(dd+3)%4][co]=step+1; 47 Node tmp; 48 tmp.x=xx; 49 tmp.y=yy; 50 tmp.dir=(dd+3)%4; 51 tmp.step=step+1; 52 tmp.color=co; 53 Q.push(tmp); 54 } 55 if(step+1<counts[xx][yy][(dd+1)%4][co]){ 56 counts[xx][yy][(dd+1)%4][co]=step+1; 57 Node tmp; 58 tmp.x=xx; 59 tmp.y=yy; 60 tmp.dir=(dd+1)%4; 61 tmp.step=step+1; 62 tmp.color=co; 63 Q.push(tmp); 64 } 65 int xxx=xx+dir[dd][0]; 66 int yyy=yy+dir[dd][1]; 67 if(xxx>=1&&xxx<=n&&yyy>=1&&yyy<=m){ 68 if(map[xxx][yyy]!='#'){ 69 if(step+1<counts[xxx][yyy][dd][(co+1)%5]){ 70 counts[xxx][yyy][dd][(co+1)%5]=step+1; 71 Node tmp; 72 tmp.x=xxx; 73 tmp.y=yyy; 74 tmp.step=step+1; 75 tmp.color=(co+1)%5; 76 tmp.dir=dd; 77 Q.push(tmp); 78 } 79 80 81 } 82 83 } 84 } 85 } 86 87 int main(){ 88 int tt=1; 89 while(~scanf("%d%d",&n,&m)&&n>0){ 90 if(tt!=1)printf("\n"); 91 for(int i=1;i<=n;i++) 92 for(int j=1;j<=m;j++){ 93 cin>>map[i][j]; 94 if(map[i][j]=='S'){ 95 sx=i; 96 sy=j; 97 } 98 if(map[i][j]=='T'){ 99 ex=i; 100 ey=j; 101 } 102 } 103 BFS(sx,sy,0,0); 104 int minn=INF; 105 printf("Case #%d\n",tt++); 106 for(int i=0;i<=3;i++){ 107 minn=min(minn,counts[ex][ey][i][0]); 108 } 109 if(minn==INF)printf("destination not reachable\n"); 110 else 111 printf("minimum time = %d sec\n",minn); 112 } 113 114 }