Hdu-1026-Ignatius and the Princess I

Source: Internet
Author: User

A n * M graph can take one step up, down, or down in one second. If there is a number, it will take time to stay in the grid, outputs the path from the start point (0, 0) to the end point (N-1, M-1) each second.

 

--> It will select the priority queue to do the job. Basically, it's okay.


[Cpp] # include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
 
Using namespace std;
 
Const int maxn = 100 + 10;
Struct node
{
Int x;
Int y;
Int step;
Int fax;
Int fay;
Node (){}
Node (int xx, int yy): x (xx), y (yy ){}
Bool operator <(const node & e) const
{
Return step> e. step;
}
Void init (int I, int j)
{
X = I;
Y = j;
Step = fax = fay =-1;
}
} No [maxn] [maxn];
Char MAP [maxn] [maxn];
Int N, M, cnt;
Int dx [] = {-1, 1, 0, 0 };
Int dy [] = {0, 0,-1, 1 };
 
Bool bfs ()
{
Priority_queue <node> pq;
Pq. push (no [0] [0]);
Node temp;
While (! Pq. empty ())
{
Temp = pq. top ();
Pq. pop ();
For (int I = 0; I <4; I ++)
{
Int newx = temp. x + dx [I];
Int newy = temp. y + dy [I];
If (newx> = 0 & newx <N & newy> = 0 & newy <M & MAP [newx] [newy]! = 'X' & no [newx] [newy]. step =-1)
{
No [newx] [newy]. step = temp. step + 1;
If (MAP [newx] [newy]! = '.') No [newx] [newy]. step + = MAP [newx] [newy]-'0 ';
No [newx] [newy]. fax = temp. x;
No [newx] [newy]. fay = temp. y;
Pq. push (no [newx] [newy]);
If (newx = N-1 & newy = M-1) return 1;
}
}
}
Return 0;
}
 
Void print (node u)
{
If (u. fax = 0 & u. fay = 0)
{
Printf ("It takes % d seconds to reach the target position, let me show you the way. \ n", no [N-1] [M-1]. step );
Printf ("% ds :( 0, 0)-> (% d, % d) \ n", cnt ++, u. x, u. y );
For (int I = 0; I <MAP [u. x] [u. y]-'0'; I ++) printf ("% ds: FIGHT AT (% d, % d) \ n", cnt ++, u. x, u. y );
Return;
}
Print (no [u. fax] [u. fay]);
Printf ("% ds :( % d, % d)-> (% d, % d) \ n", cnt ++, u. fax, u. fay, u. x, u. y );
For (int I = 0; I <MAP [u. x] [u. y]-'0'; I ++) printf ("% ds: FIGHT AT (% d, % d) \ n", cnt ++, u. x, u. y );
}
 
Int main ()
{
Int I, j;
While (scanf ("% d", & N, & M) = 2)
{
For (I = 0; I <N; I ++)
For (j = 0; j <M; j ++)
{
Cin> MAP [I] [j];
No [I] [j]. init (I, j );
}
 
No [0] [0]. step = 0;
Cnt = 1;
If (bfs () print (no [N-1] [M-1]);
Else printf ("God please help our poor hero. \ n ");
Printf ("FINISH \ n ");
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>

Using namespace std;

Const int maxn = 100 + 10;
Struct node
{
Int x;
Int y;
Int step;
Int fax;
Int fay;
Node (){}
Node (int xx, int yy): x (xx), y (yy ){}
Bool operator <(const node & e) const
{
Return step> e. step;
}
Void init (int I, int j)
{
X = I;
Y = j;
Step = fax = fay =-1;
}
} No [maxn] [maxn];
Char MAP [maxn] [maxn];
Int N, M, cnt;
Int dx [] = {-1, 1, 0, 0 };
Int dy [] = {0, 0,-1, 1 };

Bool bfs ()
{
Priority_queue <node> pq;
Pq. push (no [0] [0]);
Node temp;
While (! Pq. empty ())
{
Temp = pq. top ();
Pq. pop ();
For (int I = 0; I <4; I ++)
{
Int newx = temp. x + dx [I];
Int newy = temp. y + dy [I];
If (newx> = 0 & newx <N & newy> = 0 & newy <M & MAP [newx] [newy]! = 'X' & no [newx] [newy]. step =-1)
{
No [newx] [newy]. step = temp. step + 1;
If (MAP [newx] [newy]! = '.') No [newx] [newy]. step + = MAP [newx] [newy]-'0 ';
No [newx] [newy]. fax = temp. x;
No [newx] [newy]. fay = temp. y;
Pq. push (no [newx] [newy]);
If (newx = N-1 & newy = M-1) return 1;
}
}
}
Return 0;
}

Void print (node u)
{
If (u. fax = 0 & u. fay = 0)
{
Printf ("It takes % d seconds to reach the target position, let me show you the way. \ n", no [N-1] [M-1]. step );
Printf ("% ds :( 0, 0)-> (% d, % d) \ n", cnt ++, u. x, u. y );
For (int I = 0; I <MAP [u. x] [u. y]-'0'; I ++) printf ("% ds: FIGHT AT (% d, % d) \ n", cnt ++, u. x, u. y );
Return;
}
Print (no [u. fax] [u. fay]);
Printf ("% ds :( % d, % d)-> (% d, % d) \ n", cnt ++, u. fax, u. fay, u. x, u. y );
For (int I = 0; I <MAP [u. x] [u. y]-'0'; I ++) printf ("% ds: FIGHT AT (% d, % d) \ n", cnt ++, u. x, u. y );
}

Int main ()
{
Int I, j;
While (scanf ("% d", & N, & M) = 2)
{
For (I = 0; I <N; I ++)
For (j = 0; j <M; j ++)
{
Cin> MAP [I] [j];
No [I] [j]. init (I, j );
}

No [0] [0]. step = 0;
Cnt = 1;
If (bfs () print (no [N-1] [M-1]);
Else printf ("God please help our poor hero. \ n ");
Printf ("FINISH \ n ");
}
Return 0;
}


 

Related Article

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.