HDU 1026 Ignatius and the Princess I

Source: Internet
Author: User

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=1026

Ignatius and the Princess Idescription

The princess have been abducted by the Beelzebub feng5166 and our hero Ignatius have to rescue our pretty princess. Now he gets into feng5166 ' s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth are a n*m two-dimensional array which left-top corner is (0,0) and Righ T-bottom Corner is (n-1,m-1). Ignatius enters at (0,0), and the door to Feng5166 's are at (n-1,m-1), that's our target. There is some monsters in the castle, if Ignatius meet them, he had to kill them. Here is some rules:

1.Ignatius can only moves in four directions, one step per second. A step is defined as Follow:if current position is (x, y), after a step, Ignatius can only stand on (x-1,y), (X+1,y), (x, Y -1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X:the Place was a trap, Ignatius should not walk on it.
N:here is a monster with n HP (1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. Assume that the start position and the target position would never be a trap, and there would never be a monster at The start position.

Input

The input contains several test cases. Each test case starts with a line contains the numbers N and M (2<=n<=100,2<=m<=100) which indicate the size of The Labyrinth. Then a n*m two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

Output

For each test case, you should output "God's help to our poor hero." If Ignatius can ' t reach the target position, or you Should output "It takes n seconds to reach the target position, let me show you the The". (n is the minimum seconds), and the hero the whole path. Output a line contains the "FINISH" after all test case. If there is more than one path, any one was OK in this problem. More details in the Sample Output.

Sample Input

5 6
. Xx.1.
.. X.2.
2...X.
... Xx.
Xxxxx.
5 6
. Xx.1.
.. X.2.
2...X.
... Xx.
XXXXX1
5 6
. Xx...
.. XX1.
2...X.
... Xx.
Xxxxx.

Sample Output

It takes seconds to reach of the target position, let me show you the the The.
1s: (0, 0)--(1, 0)
2s: (1, 0)--(1, 1)
3s: (1, 1)--(2, 1)
4s: (2, 1)--(2, 2)
5s: (2, 2)--(2, 3)
6s: (2, 3)--(1, 3)
7s: (1, 3)--(1, 4)
8s:fight at (1,4)
9s:fight at (1,4)
10s: (1, 4)--(1, 5)
11s: (1, 5)--(2, 5)
12s: (2, 5)--(3, 5)
13s: (3, 5)--(4, 5)
FINISH
It takes seconds to reach the target position, let me show you the the The.
1s: (0, 0)--(1, 0)
2s: (1, 0)--(1, 1)
3s: (1, 1)--(2, 1)
4s: (2, 1)--(2, 2)
5s: (2, 2)--(2, 3)
6s: (2, 3)--(1, 3)
7s: (1, 3)--(1, 4)
8s:fight at (1,4)
9s:fight at (1,4)
10s: (1, 4)--(1, 5)
11s: (1, 5)--(2, 5)
12s: (2, 5)--(3, 5)
13s: (3, 5)--(4, 5)
14s:fight at (4,5)
FINISH
God help our poor hero.
FINISH

bfs+ Path Record:
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <queue>8#include <map>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingstd::p air; the usingstd::vector; - usingStd::multimap; - usingstd::p riority_queue; - #definePB (E) push_back (e) + #defineSZ (c) (int) (c). Size () - #defineMP (A, b) Make_pair (A, B) + #defineAll (c) (c). Begin (), (c). End () A #defineITER (c) Decltype ((c). Begin ()) at #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intMax_n = the; - Const intMod = the; intypedef unsignedLong Longull; - Const intDx[] = {0,0, -1,1}, dy[] = {-1,1,0,0 }; to BOOLVis[max_n][max_n]; + CharMaze[max_n][max_n]; - intN, M, Res, fa[max_n][max_n], dir[max_n][max_n]; the structNode { *     intx, y, S; $Node (inti =0,intj =0,intK =0): X (i), Y (j), S (k) {}Panax NotoginsengInlineBOOL operator< (ConstNode &a)Const { -         returns >A.S; the     } + }; A voidBFs () { theCLS (Vis,false), CLS (FA,0), CLS (dir,0); +Priority_queue<node>Q; - Q.push (Node ()); $      while(!Q.empty ()) { $Node T =q.top (); Q.pop (); -         if(T.x = = N-1&& T.y = = M-1) {res = T.S;return; } -Rep (I,4) { the             intNX = T.x + dx[i], NY = T.y +Dy[i]; -             Char&ch =Maze[nx][ny];Wuyi             if(NX <0|| NX >= N | | NY <0|| NY >= M)Continue; the             if(ch = ='X'|| Vis[nx][ny])Continue; -             if('0'<= CH && Ch <='9') Q.push (Node (NX, NY, T.S +1+ CH-'0')); Wu             ElseQ.push (Node (NX, NY, T.S +1)); -Vis[nx][ny] =true; AboutFa[nx][ny] = t.x * Mod +T.y; $Dir[nx][ny] =i; -         } -     } -res =-1; A } + voidShow_path (intXinty) { the     if(-1= = Res) {printf ("God Poor hero.\nfinish\n");return; } -     intFX, FY; $vector<int>path; the      for (;;) { theFX = Fa[x][y]/Mod; theFY = fa[x][y]%Mod; the         if(!x &&!y) Break; - Path.push_back (Dir[x][y]); inx = FX, y =fy; the     } theprintf"It takes%d seconds to reach the target position, let me show you the way.\n", res); AboutFX = FY =0; the      for(inti = SZ (path)-1, j =1; ~i && J <= Res; i--) { thex = FX + dx[path[i]], y = fy +Dy[path[i]]; the         Char&ch =Maze[x][y]; +         if('0'<= CH && Ch <='9') { -             intt = CH-'0'; theprintf"%ds: ( %d,%d), (%d,%d) \ n", J + +, FX, FY, x, y);Bayi              while(t--) printf ("%ds:fight at (%d,%d) \ n", J + +, x, y); the         } the         Elseprintf"%ds: ( %d,%d), (%d,%d) \ n", J + +, FX, FY, x, y); -FX = x, FY =y; -     } thePuts"FINISH"); the } the intMain () { the #ifdef LOCAL -Freopen ("In.txt","R", stdin); theFreopen ("OUT.txt","w+", stdout); the #endif the      while(~SCANF ("%d%d", &n, &M)) {94Rep (i, N) scanf ("%s", Maze[i]); the BFS (); theShow_path (N-1M1); the     }98     return 0; About}
View Code

HDU 1026 Ignatius and the Princess I

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.