Ignatius and the princess I
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 12124 accepted submission (s): 3824
Special Judge
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-dimen1_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 input5 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 outputit takes 13 seconds to reach the target position, let me show you the way.1s :()-> () 2 S :()-> () 3 s) -> () 4 s :()-> () 5 s :()-> () 6 s :()-> () 7 s) -> () 8 s: fight at () 9 S: fight at () 10 s :()-> () 11 s) -> () 12 s :()-> () 13 s :()-> () finishit takes 14 seconds to reach the target position, let me show you the way.1s :()-> () 2 S :()-> () 3 s :()-> () 4 s) -> () 5 s :()-> () 6 s :()-> () 7 s :()-> () 8 s: fight at () 9 S: fight at () 10 s :()-> () 11 s :()-> () 12 s) -> (3, 5) 13 s :( 3, 5)-> (4, 5) 14 s: fight at (4, 5) finishgod please help our poor hero. finish
Authorignatius. l simple search question: Record path search... code:
1/* problem: 1026 (Ignatius and the princess I) Judge Status: Accepted 2 runid: 11510650 language: G ++ Author: huifeidmeng 3 code render status: rendered by hdoj g ++ code render version 0.01 beta */4 5 # include <cstring> 6 # include <cstdio> 7 # include <cstdlib> 8 # include <queue> 9 # include <iostream> 10 using namespace STD; 11 const int maxn = 101; 12 struct node {13 int X, Y; 14}; 15 struct Sode {16 in T val; 17 int sum; 18 node pre; // He points 19 void setint (int x, int y) {20 pre. X = x; 21 pre. y = y; 22} 23}; 24 int n, m, cont; 25 Sode map [maxn] [maxn]; 26 int dir [4] [2] = {}, {-}, {}, {0,-1}; 27 void BFS () {28 queue <node> ANC; 29 node TEM = {0, 0}; 30 ANC. push (TEM); 31 While (! ANC. empty () {32 node sav = ANC. front (); 33 ANC. pop (); 34 for (INT I = 0; I <4; I ++) {35 TEM = (node) {dir [I] [0] + SAV. x, dir [I] [1] + SAV. y}; 36 IF (TEM. x> = 0 & TEM. x <n & TEM. y> = 0 & TEM. Y <M & map [TEM. x] [TEM. y]. val! =-1) {37 If (Map [TEM. x] [TEM. y]. sum = 038 | map [TEM. x] [TEM. y]. sum> map [Sav. x] [Sav. y]. sum + map [TEM. x] [TEM. y]. val + 1) 39 {40 map [TEM. x] [TEM. y]. sum = map [Sav. x] [Sav. y]. sum + map [TEM. x] [TEM. y]. val + 1; 41 map [TEM. x] [TEM. y]. setint (Sav. x, Sav. y); 42 ANC. push (TEM); 43} 44} 45} 46} 47} 48 void DFS (Sode A, node cur) {49 If (cur. X = 0 & cur. y = 0) {50 cont = 1; 51 while (Map [cur. x] [cur. y]. val --> 0) 52 printf ("% DS: fight at (% d, % d) \ n", cont + +, Cur. x, cur. y); 53 // printf ("% DS :( % d, % d)-> (% d, % d) \ n", cont ++,. pre. x,. pre. y, cur. x, cur. y); 54 return; 55} 56 DFS (Map [. pre. x] [. pre. y],. PRE); 57 printf ("% DS :( % d, % d)-> (% d, % d) \ n", cont ++,. pre. x,. pre. y, cur. x, cur. y); 58 if (. val> 0) {59 While (. val --> 0) {60 printf ("% DS: fight at (% d, % d) \ n", cont ++, cur. x, cur. y); 61} 62} 63} 64 int main () {65 char ss [105]; 66 // freopen ("test. in "," r ", stdin); 67 // freopen (" test. out", "W", stdout); 68 while (scanf ("% d", & N, & M )! = EOF) {69 for (INT I = 0; I <n; I ++) {70 scanf ("% s", SS); 71 for (Int J = 0; j <m; j ++) {72 map [I] [J]. sum = 0; 73 map [I] [J]. setint (0, 0); 74 if (ss [J] = '. ') map [I] [J]. val = 0; 75 else if (ss [J] = 'X') map [I] [J]. val =-1; 76 else {77 map [I] [J]. val = ss [J]-'0'; 78 if (I + J = 0) map [I] [J]. sum = map [I] [J]. val; 79} 80} 81} 82 BFS (); 83 If (Map [n-1] m-1]. pre. X = 0) 84 printf ("God please help our poor hero. \ n "); 85 else {86 printf (" it takes % d seconds to reach the target position, let me show you the way. \ n ", map [n-1] m-1]. sum); 87 DFS (Map [n-1] m-1], (node) {n-1, M-1}); 88} 89 puts ("finish"); 90} 91 return 0; 92}