HDU 1026 Ignatius and the Princess I (priority queue + print path)

Source: Internet
Author: User

Test instructions: N*m maze, from (0,0) to (n-1,m-1), Encounter monster stay monster in the box of the number of units time, to find the shortest time and print the path;

Idea: Use BFS first search the shortest road, search the shortest way must use priority queue, otherwise the result is not correct; Save the path by saving the previous step, when you reach the end, the path is queried, and the monster is in the same position;

#include <cstdio>#include<cstring>#include<queue>#include<algorithm>using namespacestd;intN,m,s1,s2,e1,e2;Charmm[505][505];intvis[505][505];intdir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};structnode{intx,y,t; BOOL operator< (ConstNODE&AMP;XX)Const    {        returnT>xx.t; }};structlujin{intX1,y1,x2,y2;} record[505];node pre[505][505];intquan[505][505],route[505][505];intBFs () {priority_queue<node>Q; VIS[S1][S2]=1;    Node Cur,now;    node S; S.x=s1;s.y=s2;s.t=0;    Q.push (s);  while(!Q.empty ()) {cur=Q.top ();        Q.pop ();  for(intI=0;i<4; i++) {now.x=cur.x+dir[i][0]; Now.y=cur.y+dir[i][1]; NOW.T=cur.t+1; if(now.x<0|| now.x>=n| | now.y<0|| now.y>=m| | vis[now.x][now.y]| | mm[now.x][now.y]=='X')Continue; if(mm[now.x][now.y]>='1'&&mm[now.x][now.y]<='9') {now.t+=mm[now.x][now.y]-'0'; }             if(now.x== (n1) &&now.y== (M-1))//get to the end, rewind to find the path            {                intnum=0, TEMP1,TEMP2; E1=now.x;e2=now.y; PRE[NOW.X][NOW.Y]=cur;  while(e1!=0|| e2!=0)//or the relationship!! Not with, small bug debug for a long time ...                 {                                    if(mm[e1][e2]>='1'&&mm[e1][e2]<='9')                    {                        for(intj=0; j<mm[e1][e2]-'0'; j + +) {record[now.t-num].x1=pre[e1][e2].x; RECORD[NOW.T-num].y1=pre[e1][e2].y; RECORD[NOW.T-num].x2=E1; RECORD[NOW.T-num].y2=E2; Num++; }} record[now.t-num].x1=pre[e1][e2].x; RECORD[NOW.T-num].y1=pre[e1][e2].y; RECORD[NOW.T-num].x2=E1; RECORD[NOW.T-num].y2=E2; Temp1=pre[e1][e2].x; Temp2=pre[e1][e2].y; //printf ("(%d,%d) (%d,%d) \ n", E1,E2,TEMP1,TEMP2);E1=temp1;e2=Temp2; Num++; }                returnnow.t; } Vis[now.x][now.y]=1;            Q.push (now); PRE[NOW.X][NOW.Y]=cur;//Save the previous step        }    }    return-1;}intMain () {intI,j,k,ans;  while(SCANF ("%d%d", &n,&m)! =EOF) {memset (mm,0,sizeof(mm)); memset (Vis,0,sizeof(VIS)); Memset (Quan,0,sizeof(Quan)); memset (PRE,0,sizeof(pre)); memset (Route,0,sizeof(route)); memset (Record,0,sizeof(record));  for(i=0; i<n;i++)        {             for(j=0; j<m;j++) {scanf ("%c",&Mm[i][j]); S1=0, s2=0; }} ans=BFS (); if(ans==-1) printf ("God Poor hero.\n"); Elseprintf"It takes%d seconds to reach the target position, let me show you the way.\n", ans); if(ans!=-1)        {             for(i=1; i<=ans;i++) {printf ("%ds:", i); if(record[i-1].x2==record[i].x2&&record[i-1].y2==record[i].y2) printf ("fight at (%d,%d) \ n", Record[i].x2,record[i].y2); Elseprintf ("(%d,%d) (%d,%d) \ n", Record[i].x1,record[i].y1,record[i].x2,record[i].y2); }} printf ("finish\n"); }}

HDU 1026 Ignatius and the Princess I (priority queue + print path)

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.