1026

1 /*Description: to
2 *
3 * Author: Vincent
4 *
5 * Date:2010/04/
6 * Contact:agilely@126.com
7 * Zju CS Lab
8
9 Problem Description
10 The 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 (0,0) 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:
11
12 1.Ignatius can only move in four directions(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).
13 2.The array is marked with some characters and numbers. We define them like this:
14 . : The place where Ignatius can walk on.
15 X : The place is a trap, Ignatius should not walk on it.
16 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.
17
18 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.
19
20
21
22 Input
23 The 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-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
24
25
26
27 Output
28 For each test case, you should output "God please help 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 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.
29
30
31
32 Sample Input
33 5 6
34 .XX.1.
35 ..X.2.
36 2...X.
37 ...XX.
38 XXXXX.
39 5 6
40 .XX.1.
41 ..X.2.
42 2...X.
43 ...XX.
44 XXXXX1
45 5 6
46 .XX...
47 ..XX1.
48 2...X.
49 ...XX.
50 XXXXX.
51
52
53 Sample Output
54 It takes 13 seconds to reach the target position, let me show you the way.
55 1s:(0,0)->(1,0)
56 2s:(1,0)->(1,1)
57 3s:(1,1)->(2,1)
58 4s:(2,1)->(2,2)
59 5s:(2,2)->(2,3)
60 6s:(2,3)->(1,3)
61 7s:(1,3)->(1,4)
62 8s:FIGHT AT (1,4)
63 9s:FIGHT AT (1,4)
64 10s:(1,4)->(1,5)
65 11s:(1,5)->(2,5)
66 12s:(2,5)->(3,5)
67 13s:(3,5)->(4,5)
68 FINISH
69 It takes 14 seconds to reach the target position, let me show you the way.
70 1s:(0,0)->(1,0)
71 2s:(1,0)->(1,1)
72 3s:(1,1)->(2,1)
73 4s:(2,1)->(2,2)
74 5s:(2,2)->(2,3)
75 6s:(2,3)->(1,3)
76 7s:(1,3)->(1,4)
77 8s:FIGHT AT (1,4)
78 9s:FIGHT AT (1,4)
79 10s:(1,4)->(1,5)
80 11s:(1,5)->(2,5)
81 12s:(2,5)->(3,5)
82 13s:(3,5)->(4,5)
83 14s:FIGHT AT (4,5)
84 FINISH
85 God please help our poor hero.
86 FINISH
87
88
89 Author
90 Ignatius.L
91
92
93 */
94 #include<iostream>
95 #include<functional>
96  using namespace std;
97 #include<queue>
98 struct Node
99 {
100 friend bool operator<(Node n1,Node n2)
101 {
102 return n1.t > n2.t;
103 }
104 int x;
105 int y;
106 int t;
107 struct Node *prev;
108 };
109 Node N[10003],P;
110 bool success;
111 int w;
112 int dir[][2]={{1,0},{0,1},{-1,0},{0,-1}};
113 char map[101][101];
114 int mark[101][101],n,m;
115 int _x[1001],_y[1001];
116 int main()
117 {
118 void bfs();
119 while(scanf("%d%d",&n,&m)!=EOF)
120 {
121 int i;
122 for(i=0;i<n;i++)
123 cin>>map[i];
124 success=false;
125 bfs();
126 if(success)
127 {
128 printf("It takes %d seconds to reach the target position, let me show you the way.\n",N[w].t);
129 int len=N[w].t;
130 _x[len]=N[w].x;_y[len]=N[w].y;
131 Node *p;
132 p=&N[w];
133 int b=len;
134 while(1)
135 {
136 p=p->prev;
137 if(p==NULL)
138 break;
139 b--;
140 _x[b]=(*p).x;
141
142 _y[b]=(*p).y;
143
144 }
145 int o=1;
146
147 for(i=b;i<=len-1;i++)
148 {
149
150 if(map[_x[b+1]][_y[b+1]]=='.')
151 {
152 printf("%ds:(%d,%d)->(%d,%d)\n",o,_x[b],_y[b],_x[b+1],_y[b+1]);
153 b++;
154 o++;
155 }
156 else if(map[_x[b+1]][_y[b+1]]!='.')
157 {
158 printf("%ds:(%d,%d)->(%d,%d)\n",o,_x[b],_y[b],_x[b+1],_y[b+1]);
159 int v=o;
160 for( o=o+1; o<v+1+map[_x[b+1]][_y[b+1]]-'0';o++)
161 {
162 printf("%ds:FIGHT AT (%d,%d)\n",o,_x[b+1],_y[b+1]);
163 }
164 b++;
165 }
166
167 }
168
169 }
170 else
171 printf("God please help our poor hero.\n");
172 printf("FINISH\n");
173 }
174 }
175
176 void bfs()
177 {
178 memset(mark,0,sizeof(mark));
179 priority_queue<Node>Q;
180 N[1].t=0;N[1].x=0;N[1].y=0;N[1].prev=NULL;
181 mark[0][0]=1;
182 Q.push(N[1]);
183 w=2;
184 while(!Q.empty())
185 {
186
187 N[w]=Q.top();
188 Q.pop();
189 if(N[w].x==n-1&&N[w].y==m-1)
190 {
191 success=1;
192 break;
193 }
194 for(int i=0;i<4;i++)
195 {
196 int tx=N[w].x+dir[i][0];
197 int ty=N[w].y+dir[i][1];
198 if(tx>=0 && tx<n && ty>=0 && ty<m && !mark[tx][ty])
199 {
200 if(map[tx][ty]!='X')
201 {
202 P.x=tx;P.y=ty;P.prev=&N[w];
203 mark[tx][ty]=1;
204 if(map[tx][ty]=='.')
205 {
206 P.t=N[w].t+1;
207 Q.push(P);
208 }
209 if(map[tx][ty]!='.')
210 {
211 P.t=N[w].t+1+map[tx][ty]-'0';
212 Q.push(P);
213 }
214 }
215 }
216 }
217 w++;
218 }
219
220 }
221
222

 

posted @ 2010-04-15 23:26  にんじゃ  阅读(305)  评论(0)    收藏  举报