Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1242
Test instructions: "R" is the starting point, "a" is the end point, '. ' Is the road, ' X ' is the Guard, ' # ' is not the place to go. Kill the guard to spend more than 1 units of time, each move up or down to spend a unit of time. Ask for the least amount of time to finish.
Idea: This n*m map and take the right (one is 1, one is 2) of the general use of bfs+ priority queue, if not with the right can be directly used BFS.
1#include <bits/stdc++.h>2 using namespacestd;3 intN, M;4 #defineMAXN 2105 #defineINF 0x3f3f3f3f6 CharMP[MAXN][MAXN];7 intSX, SY, ex, EY;8 intdir[4][2] = {{-1,0}, {1,0}, {0, -1}, {0,1}};9 structNodeTen { One intx, y, D; ANode (intXxintYyintdd) - { -x = XX; y = yy; D =DD; the } - Node () {} -FriendBOOL operator<(Node A, Node B) - { + returnA.D >B.D; - } + }; A intDIST[MAXN][MAXN]; at intBFS () - { -memset (Dist, INF,sizeof(Dist)); -Priority_queue <Node>Q; -Dist[sx][sy] =0; -Q.push (SX, SY,0)); in while(!q.empty ()) - { toNode U =q.top (); Q.pop (); + for(inti =0; I <4; i++) - { the intxx = u.x + dir[i][0]; * intyy = U.y + dir[i][1]; $ //cout<<xx<< "" <<yy<<endl;Panax Notoginseng if(XX <0|| XX >= N | | yy <0|| yy >= M | | MP[XX][YY] = ='#')Continue; - if(Mp[xx][yy] = ='a')returnU.d+1; the +Node temp =Node (xx, yy, u.d); Atemp.d++; the if(Mp[xx][yy] = ='x') temp.d++; + if(Dist[xx][yy] >=temp.d) - { $ Q.push (temp); $DIST[XX][YY] =TEMP.D; - } - } the } - return-1;Wuyi the } - intMain () Wu { - //freopen ("In.txt", "R", stdin); About //freopen ("OUT.txt", "w", stdout); $ while(~SCANF ("%d%d", &n, &M)) - { - for(inti =0; i < N; i++) - { Ascanf"%s", Mp[i]); + } the for(inti =0; i < N; i++) - { $ for(intj =0; J < M; J + +) the { the if(Mp[i][j] = ='R') {sx = i; sy =J;} the if(Mp[i][j] = ='a') {ex = i; ey =J;} the } - } in intAns =BFS (); the if(ans = =-1) printf ("Poor ANGEL have to stay in the prison all his life.\n"); the Elseprintf"%d\n", ans); About } the return 0; the}
HDU 1242 Rescue bfs+ Priority queue