The main idea: there is a n*m grid, there are some places on the grid there are obstacles
Now there is a man, riding a wheelbarrow, asking from one place to another, when riding a wheelbarrow, you can only go straight, or turn left, turn right, can't go backwards
The wheel of a wheelbarrow is divided into 5 parts, each of which has a corresponding color, the first is green downward, and when passed a lattice, the color will change
Ask from the starting point to the end point, to the end of the wheel car green color downward, how long it takes
Problem-solving ideas: violent BFS
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace STD;#define Nstructnode{intX, Y, dir, time, color;} Startintdir[4][2] = {{-1,0}, {0,1}, {0, -1}, {1,0}};intG[n][n], vis[n][n][5][4];CharStr[n];intend_x, End_y, N, M;voidInit () { for(inti =0; I < n; i++) {scanf('%s ', str); for(intj =0; J < M; J + +) {if(Str[j] = =' # ') {G[i][j] =1; }Else{G[i][j] =0;if(Str[j] = =' S ') {start.x = i; Start.y = j; }if(Str[j] = =' T ') {end_x = i; End_y = j; } } } }memset(Vis,0,sizeof(VIS));}voidSolve () { queue<Node>Q Start.color =0; Start.time =0; Start.dir =0; Q.push (start); vis[start.x][start.y][0][0] =1; while(!q.empty ()) {Node T = Q.front (); Q.pop ();if(T.x = = end_x && T.y = = End_y && T.color = =0) {printf("Minimum time =%d sec\n", t.time);return; } for(inti =0; I <4; i++) {if(T.dir + i = =3)Continue; Node tt; Tt.x = T.x; Tt.y = T.y; Tt.color = T.color; Tt.time = T.time +1; Tt.dir = T.dir;if(Tt.dir = = i) {Tt.x + = dir[tt.dir][0]; Tt.y + = dir[tt.dir][1]; Tt.color = (Tt.color +1) %5;if(Tt.x <0|| Tt.x >= N | | Tt.y <0|| Tt.y >= m | | G[TT.X][TT.Y] | | Vis[tt.x][tt.y][tt.color][tt.dir])Continue; Vis[tt.x][tt.y][tt.color][tt.dir] =1; Q.push (TT); }Else{Tt.dir = i;if(!vis[tt.x][tt.y][tt.color][tt.dir]) {Vis[tt.x][tt.y][tt.color][tt.dir] =1; Q.push (TT); } } } }printf("Destination not reachable\n");}intMain () {intCAS =1;intFlag =0; while(scanf("%d%d", &n, &m)! = EOF && n + m) {if(flag)printf("\ n");ElseFlag =1; Init ();printf("Case #%d\n", cas++); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-10047 the Monocycle (BFS)