This topic is very bumpy ah, WA a lot of times, but all of the ideas are running wide search to think, the first appeared than the answer big data, just think of should be priority queue, say plus also certainly not wrong. At first I read the wrong test instructions and thought it would take time to rotate and move forward, but the action of rotation would require extra timing. My first method of the wrong reason has not been found, I have changed the position after the rotation, the feeling there is no essential difference, after the rotation, definitely to go ah, I directly add time also no problem, I also think of the test cases have tried, and the AC code exactly, really do not understand, this if CF is good. So, learning someone else's code, changed my original place, found that there is really wrong. After a few more studies, just calculate the elegant AC of the topic.
The Spit groove is here, let me take a look at the code.
The state uses four-dimensional array to save, the position occupies two dimensions, the direction and the color each occupies one dimension, the color is the title gives the 5 kinds of colors, the direction best given a marking, facilitates later walking. (0N, 1E, 2s,3w), in the search process, each operation will be spent 1 seconds, the operation is divided into the left and right rotation, that is, change dir, moving in the current direction, change x and Y. Each time the 3 new state into the priority queue, the choice of time-consuming as a team head, so when the search, is the smallest number of steps.
#include <iostream>#include<cstdio>#include<map>#include<queue>#include<cstring>using namespacestd;#defineMAXN 30structnode{intX,y,col,dir,tim; BOOL operator< (Node a)Const{ returnA.tim <Tim; }};CharMAPS[MAXN][MAXN];intstate[maxn][maxn][Ten][Ten],n,m,go[4][2]={{-1,0},{0,1},{1,0},{0,-1}};///Note that the array is handled in the direction you specify, making it easy to walkPriority_queue<node>que;BOOLOK (Node a) {if(a.x>=0&&a.x<n && a.y>=0&&a.y<m && state[a.x][a.y][a.col][a.dir]==0&& maps[a.x][a.y]!='#')return true; return false;}voidMark (Node a) {State[a.x][a.y][a.col][a.dir]=1;}intBFS (Node s) { while(!que.empty ()) Que.pop (); Que.push (s); State[s.x][s.y][s.col][s.dir]=1; while(!Que.empty ()) {Node now=Que.top (); Que.pop (); if(maps[now.x][now.y]=='T'&& now.col==0)returnNow.tim; Node NXT=Now ; Nxt.tim++;///consumes 1s per operationNxt.dir = (Now.dir +1)%4; if(OK (NXT)) {Que.push (NXT); Mark (NXT);} ///the first one turns to the right, notice that the turnNxt.dir= Now.dir-1;if(nxt.dir<0) nxt.dir=3; if(OK (NXT)) {Que.push (NXT); Mark (NXT);} ///turn left at the second wayNxt.dir=Now.dir; Nxt.col= (now.col+1)%5; Nxt.x= go[now.dir][0] +now.x; Nxt.y= go[now.dir][1] +now.y; ///Walk in the direction and change the color if(OK (NXT)) {Que.push (NXT); Mark (NXT);} } return-1;}intMain () {intCA =0; while(EOF! = scanf ("%d%d",&n,&m)) { if(n==0&& m==0) Break; if(CA) printf ("\ n");///Be careful not to export more empty lines for(inti =0; I < n;i++) scanf ("%s", Maps[i]); Node start; memset (state,0,sizeof(state)); for(inti =0; I < n;i++){ for(intj =0; J < m;j++){ if(maps[i][j]=='S') {Start.x= i; Start.y =J; Start.col=0; Start.dir=0; Start.tim=0; } } } intAns =BFS (start); printf ("Case #%d\n",++CA); if(ans = =-1) printf ("Destination not reachable\n"); Elseprintf"Minimum time =%d sec\n", ans); } return 0;}
Uvalive 2035The monocycle (BFS status processing + priority queue)