8
The problem is based on the maze of some changes, that the map can walk on the point may be time-consuming 1, it may take time 2. Then, elements in the out of the queue, not simply in accordance with the previous order out of the team, and should let the shortest time first out of the team, so that you can ensure that the first point must be the shortest time, then the end of the search time also must be minimal. Now looking back, the maze problem is not considered this problem, because the first team to the point of time must not be greater than the team after the queue.
So, how to make the shortest time first out of the team? ----------STL has helped us out,
Priority_queue;
Priority Queue +bfs
Overloaded method One:
1 structnode2 {3 intx, y;4 intStep;5 };6priority_queue<node>q;//The comparison rules of the elements in the priority queue are sorted by the value of the elements from large to small by default;7 8 BOOL operator< (ConstNode &a,ConstNode &b)//the parentheses are const and must also be references9 {Ten returnA.step > B.step;//sort from small to large. Overload less than sign. Because the default is from big to small One}
Overloaded Method Two:
1 structnode2 {3 intx, y;4 intStep//define a priority queue5FriendBOOL operator<(Node A, Node B)6{//The " >" is used from small to large, and if you want to sort from large to small, the "<" number7 returnA.step > B.step;//sort from small to large8 }9 }; Tenpriority_queue<node>q;//The comparison rules of the elements in the priority queue are sorted by the value of the elements from large to small by default;
Remember: from small to large sorted by the ">", if you want to sort from large to small, the "<" number;
AC Code:
1#include <cstdio>2#include <queue>3#include <string.h>4 using namespacestd;5 #defineMax 3106 intN, M, SX, SY, ex, EY;7 Charmap[305][305];8 intdx[4]={0,0,1, -1};9 intdy[4]={1, -1,0,0};Ten structnode{ One intx, y, step; AFriendBOOL operator<(Node A, Node B) - { - returnA.step > B.step;//the number of steps is less priority the } - }a, B; - intJudgeintXinty) - { + if(X <0|| X >= m | | Y <0|| Y >=N) - return false; + if(Map[x][y] = ='R'|| Map[x][y] = ='S') A return false; at return true; - } - BOOLBFS () - { - intFlag =0; -Priority_queue<node>Q; ina.x = SX; A.Y = sy; A.step =0; - Q.push (a); to while(!q.empty ()) + { -b =q.top (); the Q.pop (); * if(b.x = = Ex && b.y = =ey) ${flag =1; Break; }Panax Notoginseng for(intK =0; K <4; k++) - { thea.x = b.x +Dx[k]; +A.Y = B.y +Dy[k]; A if(judge (a.x, A.Y)) the { + if(MAP[A.X][A.Y] = ='B') -A.step = B.step +2; $ Else $A.step = B.step +1; -MAP[A.X][A.Y] ='S'; - Q.push (a); the } - } Wuyi } the if(flag) printf ("%d\n", b.step); - Elseprintf"-1\n"); Wu } - intMain () About { $ while(SCANF ("%d%d", &m, &n)! =EOF) - { - if(m + N = =0) Break; - for(inti =0; I < m; i++) A { +scanf"%s", &map[i]); the for(intj =0; J < N; J + +) - { $ if(Map[i][j] = ='Y') the{sx = i; sy =J; } the if(Map[i][j] = ='T') the{ex = i; ey =J; } the } - } in BFS (); the } the return 0; About}