Previously written questions, now want to tidy up, hanging out.
Test instructions more clear, give a map of n*m, from the upper left corner (0, 0) to the lower right corner (n-1, M-1).
' X ' for the Wall, '. ' For the road, the number for the monster. The wall cannot walk, the road spends 1s passes, the monster needs to spend 1s+ the number size time.
The trouble is that you need to record the path. Also keep track of whether you are walking or playing strange.
Because of the shortest path, BFS can be used.
The priority queue can be used because it takes a different amount of time to go through each point.
Because you need to record the path, you need to open an array to record the parent node that passes through the node. Of course, there are more than one recording method.
On the code--
1#include <cstdio>2#include <cstring>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <queue>7 using namespacestd;8 9 structnodeTen { One intx, y, step; A BOOL operator< (Constnode& a)Const - { - returnA.step <Step; the } - }; - - intHot4][2] = {{1,0},{-1,0}, {0,1}, {0, -1}}; + - intN, M, step; + BOOLv[ the][ the]; A Charmp[ the][ the]; at intlast[ the][ the]; - - BOOLBFS () - { - node p, q; -p.x = n1; inP.Y = m1; -P.step =0; to if(mp[n-1][m-1] >='0'&& mp[n-1][m-1] <='9') P.step + = mp[n-1][m-1] -'0'; + -Priority_queue <node>que; the if(MP[P.X][P.Y]! ='X') * Que.push (p); $V[P.X][P.Y] =1;Panax Notoginseng - while(!que.empty ()) the { +p =que.top (); A Que.pop (); the + for(inti =0; I <4; i++) - { $ intx = p.x+go[i][0]; $ inty = p.y+go[i][1]; - - if(x >=0&& x < n && y >=0&& y < m &&!V[x][y]) the { - if(Mp[x][y] = ='X')Continue;Wuyi theq.x = x; Q.y = y; Q.step = p.step+1; - if(Mp[x][y] >='1'&& Mp[x][y] <='9') Q.step + = mp[x][y]-'0'; Wu Que.push (q); -V[x][y] =1; AboutLast[x][y] = p.x* ++p.y; $ if(x = =0&& y = =0) {step = Q.step;return 1;} - - } - } A } + return 0; the } - $ voidoutput () the { theprintf"It takes%d seconds to reach the target position, let me show you the way.\n", step); the intx =0; the inty =0; - inti =1; in while(x! = N1|| Y! = m1) the { the if(Mp[x][y] >='1'&& Mp[x][y] <='9') About { the intStop = Mp[x][y]-'0'; the while(stop--) the { +printf"%ds:fight at (%d,%d) \ n", i++, x, y); - } the }Bayiprintf"%ds: (%d,%d) (%d,%d) \ n", i++, X, Y, last[x][y]/ +, last[x][y]% +); the the intt = last[x][y]/ +; -y = last[x][y]% +; -x =T; the } the if(Mp[x][y] >='1'&& Mp[x][y] <='9') the { the intStop = Mp[x][y]-'0'; - while(stop--) the { theprintf"%ds:fight at (%d,%d) \ n", i++, x, y); the }94 } the } the the intMain ()98 { About //freopen ("Test.txt", "R", stdin); - while(~SCANF ("%d%d", &n, &m))101 {102Memset (MP,0,sizeof(MP));103memset (V,0,sizeof(v));104Memset (Last,0,sizeof(last)); the for(inti =0; I < n; i++)106 {107scanf"%s", Mp[i]);108 }109 the if(BFS ()) output ();111 Elseprintf"God Poor hero.\n"); theprintf"finish\n");113 } the return 0; the}
View Code
HDU 1026 Ignatius and the Princess I (priority queue +bfs+ record Path)