HDU 1026-Ignatius and the princess

Source: Internet
Author: User
Problem descriptionthe princess has been abducted by the Beelzebub feng5166, our hero Ignatius has 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 is a n * m two-dimensional
Array which left-top corner is () and right-bottom corner is (N-1, M-1 ). ignatius enters at (0, 0), and the door to feng5166's room is at (N-1, M-1), that is our target. there are some monsters in the castle, if Ignatius meet them, he has to kill them. here
Is some rules:

1. ignatius can only move in four directions ctions (Up, down, left, right), 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 is a trap, Ignatius shocould 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. you may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position. inputthe input contains several test cases. each test case starts with a line contains two numbers N and M (2 <= n <= 100,2 <= m <= 100) which indicate the size of the labyrinth. then a n * m two-dimen1_array follows, which describe the whole labyrinth. the input is terminated
By the end of file. more details in the sample input. outputfor each test case, You shoshould output "God please help our poor hero. "If Ignatius can't reach the target position, or you shoshould output" it takes n seconds to reach the target position, let me show you the way. "(n is the minimum seconds), and tell our hero
The whole path. output A line contains "finish" after each test case. if there are more than one path, any one is 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.XXXXX15 6.XX.....XX1.2...X....XX.XXXXX.
Sample output
It takes 13 seconds to reach the target position, let me show you the way.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)FINISHIt takes 14 seconds to reach the target position, let me show you the way.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)FINISHGod please help our poor hero.FINISH
It is a difficult search question! Refer to haichao's code ....
# Include <iostream> using namespace STD; # define Maxx 10010 char map [110] [110]; int vis [110] [110], F [110] [110]; struct node {int X, Y, T; node () {} node (int A, int B, int C): X (A), y (B ), T (c) {}} path [Maxx], que [Maxx * 9]; int n, m; int FX [] = {1,-1, 0 }; int FY [] = {0, 0, 1,-1}; int BFS () {int I, j, T, step; memset (VIS,-1, sizeof (VIS); for (I = 0; I <n; I ++) {scanf ("% s", map [I]); For (j = 0; j <m; j ++) if (Map [I] [J] = '. ') map [I] [J] = '0';} int S, E; que [S = E = 0] = node (0, 0, map [0] [0]-'0'); vis [0] [0] = 0; while (S <= e) {node NO = que [s ++]; If (No. t) {// It's strange to stay in the same place and press que [++ E] = node (No. x, No. y, no. t-1); vis [No. x] [No. y] ++; continue;} If (n-1 = No. X & M-1 = No. y) break; // jump out of the For (INT I = 0; I <4; I ++) {int Nx = No. X + FX [I], NY = No. Y + FY [I]; If (nx <0 | ny <0 | NX> = n | ny> = m) continue; if (Map [NX] [NY] = 'X' | Vis [NX] [NY]! =-1) continue; vis [NX] [NY] = vis [No. x] [No. y] + 1; F [NX] [NY] = I; // mark the direction que [++ E] = node (NX, NY, map [NX] [NY]-'0');} return vis [n-1] m-1];} int main () {int X, Y, NX, NY, i, j, Tol; while (~ Scanf ("% d", & N, & M) {Tol = BFS (); If (Tol =-1) puts ("God please help our poor hero. "); else {int step = 0; X = n-1, y = s-1; while (X | Y) {// direction recorded through F, we can know from which coordinates it is taken to this coordinate, and export the route to path [step ++] = node (x, y, map [x] [Y]-'0'); Nx = x + FX [f [x] [Y] ^ 1], NY = Y + FY [f [x] [Y] ^ 1]; X = NX, y = NY;} path [STEP] = node (0, 0, map [0] [0]-'0'); printf ("it takes % d seconds to reach the target position, let me show you the way. \ n ", Tol); int T = 0; while (step --) {printf (" % DS :( % d, % d)-> (% d, % d) \ n ", ++ T, path [step + 1]. x, path [step + 1]. y, path [STEP]. x, path [STEP]. y); While (path [STEP]. t --) printf ("% DS: fight at (% d, % d) \ n", ++ T, path [STEP]. x, path [STEP]. y) ;}} puts ("finish");} return 0 ;}

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.