HDU 1026 bsf+ Priority queue + record path,

Source: Internet
Author: User

#include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include < algorithm> #include <queue> #include <stack>using namespace std;const int Qq=110;const int Max=10000;char  Map[qq][qq];int pre[qq][qq];int a[max],b[max];int n,m;int dir[5][2]={0,0,1,0,-1,0,0,1,0,-1};                 The order of walking is right, left, bottom, upper struct node{int x,y;int pre;                  Defines the direction of the source 1 left 2 right 3 on 4 under Char mark;    Is there a monster int time;friend bool operator < (Node A,node b) {return a.time>b.time; }};p riority_queue<node>q;int check (int x,int y) {if (x<0| | y<0| | x>=n| | Y&GT;=M)//To determine whether to cross the return 0;if (Pre[x][y])//To determine whether to pass this return 0;if (map[x][y]== ' x ')//To determine whether the wall return 0;return 1;} void BFs () {Node ans,cns;ans.x=0;ans.y=0;ans.pre=0;ans.time=0;ans.mark=map[0][0]; Q.push (ANS); while (! Q.empty ()) {cns=q.top (); Q.pop ();//printf ("%d%d\n", cns.x,cns.y), if (cns.x==n-1&&cns.y==m-1) {//printf ("%d%d\n", cns.x,cns.y); while (! Q.empty ()) Q.pop (); Q.push (CNS); break;}for (int i=1;i<=4;++i) {ans.x=cns.x+dir[i][1];ans.y=cns.y+dir[i][0];if (check (ANS.X,ANS.Y)) {ans.mark=map[ans.x ][ans.y];ans.pre=i;pre[ans.x][ans.y]=i;if (map[ans.x][ans.y]>= ' 1 ' &&map[ans.x][ans.y]<= ' 9 ') ans.time=cns.time+1+ (map[ans.x][ans.y]-' 0 '); elseans.time=cns.time+1; Q.push (ANS);}} return;} void out () {Node ans;ans=q.top (); Q.pop ();p rintf ("It takes%d seconds to reach the target position, let me show you the way.\n", ans.time); int r=1;a[0]=ans.x ; B[0]=ans.y;int x=ans.x,y=ans.y;//printf ("%d%d\n", x, y); while (x!=0| | y!=0) {//X and Y were reversed for two hours, akzent drunk if (pre[x][y]==1) Y=y-1;else if (pre[x][y]==2) Y=y+1;else if (pre[x][y]==3) X=x-1;else if (pre [x] [Y]==4] x=x+1;a[r]=x;b[r++]=y;//printf ("%d%d\n", x, y);} int t=1; for (int i=r-2;i>=0;--i) {printf ("%ds: (%d,%d) (%d,%d) \ n", t++,a[i+1],b[i+1],a[i],b[i]); if (Map[a[i]][b[i]] >= ' 1 ' &&map[a[i]][b[i]]<= ' 9 ') for (int. j=0;j<map[a[i]][b[i]]-' 0 '; ++j) printf ("%ds:fight at (%d,%d) \ n ", T++,a[i],b[i]);}} int main () {while (~SCANF ("%d%d",&n,&m) {memset (pre,0,sizeof (PRE)), memset (A,0,sizeof (a)), memset (b,0,sizeof (b)), for (int i=0;i<n;++i) scanf ("%s", map [i]); BFS (), if (Q.empty ()) printf ("God's help to our poor hero.\n"), Else Out ();p rintf ("finish\n"); Q.empty ()) Q.pop ();} return 0;} The array of record paths is small and has been tuned for a long time!!  Qaq

HDU 1026 bsf+ Priority queue + record 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.