HDU 1242 Rescue bfs+ Priority queue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.