hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10312 Accepted Submission(s): 3125 Special Judge
Problem Description
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:
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). 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 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. 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.
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). 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 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. 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.
Input
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.
Output
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.
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.
.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 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)
FINISH
It 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)
FINISH
God please help our poor hero.
FINISH
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 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)
FINISH
God please help our poor hero.
FINISH
Author
Ignatius.L
1 /* 2 3 搜索,输出走的路径。 4 用一个falg数组保存每一次的方向,所以最后 递归的求出来就可以了。 5 6 优先队列的两种不同写法。 7 在搜索的过程中,不能出现走回头路的情况。这里使用了一个把后路堵死的方法。 8 “f[x1][y1]=-1;//防止重复访问。” 9 而且优先队列,对于一个点来说,第一次访问的time就是最小的。 10 11 12 */ 13 14 #include<iostream> 15 #include<cstdio> 16 #include<cstdlib> 17 #include<cstring> 18 #include<queue> 19 using namespace std; 20 int n,m,time1,TT; 21 int f[102][102]; 22 int cnt[102][102]; 23 int flag[102][102]; 24 int map1[4][2]={ {1,0},{0,1},{-1,0},{0,-1} }; 25 struct node 26 { 27 int x,y; 28 int t; 29 }; 30 struct cmp 31 { 32 bool operator() (const node &n1,const node &n2) //!!注意有的不是这样写的。 33 { 34 return n1.t>n2.t; 35 } 36 }; 37 38 int bfs() 39 { 40 int i,x1,y1; 41 node next,cur; 42 priority_queue<node,vector<node>,cmp>Q;//! 43 cur.x=1; cur.y=1; cur.t=0; 44 f[1][1]=-1; 45 Q.push(cur); 46 while(!Q.empty()) 47 { 48 cur=Q.top(); 49 Q.pop(); 50 for(i=0;i<4;i++) 51 { 52 x1=cur.x+map1[i][0]; 53 y1=cur.y+map1[i][1]; 54 next.x=x1; 55 next.y=y1; 56 if(x1>=1&&x1<=n &&y1>=1&&y1<=m && f[x1][y1]!=-1) 57 { 58 next.t=cur.t+f[x1][y1]+1; 59 Q.push(next); 60 f[x1][y1]=-1;//防止重复访问。 61 flag[x1][y1]=i+1;//记录 路径 62 if(x1==n && y1==m) 63 return next.t; 64 } 65 } 66 } 67 return 0; 68 } 69 70 void print(int x,int y) 71 { 72 int x1,y1; 73 if(flag[x][y]==0)return; 74 x1=x-map1[flag[x][y]-1][0]; 75 y1=y-map1[flag[x][y]-1][1]; 76 77 print(x1,y1); 78 printf("%ds:(%d,%d)->(%d,%d)\n",TT++,x1-1,y1-1,x-1,y-1); 79 while(cnt[x][y]) 80 { 81 printf("%ds:FIGHT AT (%d,%d)\n",TT++,x-1,y-1); 82 cnt[x][y]--; 83 } 84 return; 85 } 86 87 int main() 88 { 89 int i,j,Sum; 90 char a[104]; 91 while(scanf("%d%d",&n,&m)>0) 92 { 93 memset(cnt,0,sizeof(cnt)); 94 memset(flag,0,sizeof(flag)); 95 for(i=1;i<=n;i++) 96 { 97 scanf("%s",a+1); 98 for(j=1;j<=m;j++) 99 { 100 if(a[j]=='X') 101 f[i][j]=-1; 102 else if(a[j]=='.') 103 f[i][j]=0; 104 else f[i][j]=cnt[i][j]=a[j]-'0'; 105 } 106 } 107 TT=1; 108 Sum=bfs(); 109 if(Sum) 110 { 111 printf("It takes %d seconds to reach the target position, let me show you the way.\n",Sum); 112 print(n,m); 113 } 114 else 115 printf("God please help our poor hero.\n"); 116 117 printf("FINISH\n"); 118 } 119 return 0; 120 }