此题需要时间更少,控制时间很要,这个题目要多多看,
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9910 Accepted Submission(s): 2959 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.
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)
1
4s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
Author
Ignatius.L
1 1 #include <stdio.h> 2 2 #define MAX_ 100000; 3 3 char map[100][100]; 4 4 typedef struct { 5 5 int x ,y ,time ,front; 6 6 }point; 7 7 int dir[4][2] = {-1,0,0,1,1,0,0,-1},mintime[100][100]; 8 8 int n , m ,I ,J; 9 9 point arr[1000000]; 10 10 void bfs () 11 11 { 12 12 while (I != J)//队列不为空 13 13 { 14 14 point head = arr[I++]; 15 15 for (int i = 0 ;i <= 3 ;i++) 16 16 { 17 17 int x = head.x + dir[i][0] ,y = head.y + dir[i][1]; 18 18 if (x >= 0 && y >= 0 && x < m && y < n && map[x][y] != 'X') 19 19 { 20 20 point k; 21 21 //k.x = x ,k.y = y; 22 22 k.time = head.time + 1; 23 23 if (map[x][y] != '.') 24 24 k.time += map[x][y] - '0'; 25 25 if(k.time < mintime[x][y])//若到达minttime【x】【y】时间小于其他点到此点时间,此点入队 26 26 { 27 27 mintime[x][y] = k.time; 28 28 k.x = x ,k.y = y; 29 29 k.front = I - 1; 30 30 //printf("%d---%d---%d---%d ",k.x ,k.y ,k.time ,k.front); 31 31 arr[J++] = k; 32 32 } 33 33 } 34 34 35 35 } 36 36 } 37 37 } 38 38 int main() 39 39 { 40 40 while (scanf("%d %d",&m,&n) != EOF) 41 41 { 42 42 for (int i = 0 ;i < m ;i++) 43 43 for (int j = 0 ;j < n ;j++) 44 44 { 45 45 scanf(" %c",&map[i][j]); 46 46 mintime[i][j] = MAX_; 47 47 } 48 48 //队列第一个元素赋值 49 49 arr[0].x = m - 1 ,arr[0].y = n - 1 ,arr[0].time = 0 ,arr[0].front = -1 ,mintime[m-1][n-1] = 0; 50 50 if (map[m-1][n-1] != 'X' && map[m-1][n-1] != '.') mintime[m - 1][n - 1] = arr[0].time = map[m-1][n-1] - '0'; 51 51 I = 0 ,J = 1; 52 52 int time = 1; 53 53 //执行搜索 54 54 bfs (); 55 55 //不能到达 56 56 if (mintime[0][0] == 100000) 57 57 printf ("God please help our poor hero. "); 58 58 else 59 59 { 60 60 printf ("It takes %d seconds to reach the target position, let me show you the way. ",mintime[0][0]); 61 61 point s = arr[I-1]; 62 62 while (s.x != 0 || s.y !=0) 63 63 s = arr[--I]; 64 64 int p ,time = 1 ,x ,y; 65 65 //打印路径 66 66 while (s.front >= 0) 67 67 { 68 68 p = s.front; 69 69 x = arr[p].x ,y = arr[p].y; 70 70 printf ("%ds:(%d,%d)->(%d,%d) ",time++,s.x,s.y,x,y); 71 71 if (map[x][y] != 'X' && map[x][y] != '.') 72 72 for(int i = 1 ;i <= map[x][y]-'0';i++) 73 73 printf("%ds:FIGHT AT (%d,%d) ",time++,x,y); 74 74 s = arr[p]; 75 75 } 76 76 } 77 77 printf ("FINISH "); 78 78 } 79 79 return 0; 80 80 } 81 81 方法2: 82 82 #include <stdio.h> 83 83 #define MAX_ 100000; 84 84 char map[100][100]; 85 85 typedef struct { 86 86 int x ,y ,time ,front; 87 87 }point; 88 88 int dir[4][2] = {-1,0,0,1,1,0,0,-1},mintime[100][100]; 89 89 int n , m ,I ,J; 90 90 point arr[1000000]; 91 91 void bfs () 92 92 { 93 93 while (I != J)//队列不为空 94 94 { 95 95 point head = arr[I++]; 96 96 for (int i = 0 ;i <= 3 ;i++) 97 97 { 98 98 int x = head.x + dir[i][0] ,y = head.y + dir[i][1]; 99 99 if (x >= 0 && y >= 0 && x < m && y < n && map[x][y] != 'X') 100 100 { 101 101 point k; 102 102 //k.x = x ,k.y = y; 103 103 k.time = head.time + 1; 104 104 if (map[x][y] != '.') 105 105 k.time += map[x][y] - '0'; 106 106 if(k.time < mintime[x][y])//若到达minttime【x】【y】时间小于其他点到此点时间,此点入队 107 107 { 108 108 mintime[x][y] = k.time; 109 109 k.x = x ,k.y = y; 110 110 k.front = I - 1; 111 111 //printf("%d---%d---%d---%d ",k.x ,k.y ,k.time ,k.front); 112 112 arr[J++] = k; 113 113 } 114 114 } 115 115 116 116 } 117 117 } 118 118 } 119 119 int main() 120 120 { 121 121 while (scanf("%d %d",&m,&n) != EOF) 122 122 { 123 123 for (int i = 0 ;i < m ;i++) 124 124 for (int j = 0 ;j < n ;j++) 125 125 { 126 126 scanf(" %c",&map[i][j]); 127 127 mintime[i][j] = MAX_; 128 128 } 129 129 //队列第一个元素赋值 130 130 arr[0].x = m - 1 ,arr[0].y = n - 1 ,arr[0].time = 0 ,arr[0].front = -1 ,mintime[m-1][n-1] = 0; 131 131 if (map[m-1][n-1] != 'X' && map[m-1][n-1] != '.') mintime[m - 1][n - 1] = arr[0].time = map[m-1][n-1] - '0'; 132 132 I = 0 ,J = 1; 133 133 int time = 1; 134 134 //执行搜索 135 135 bfs (); 136 136 //不能到达 137 137 if (mintime[0][0] == 100000) 138 138 printf ("God please help our poor hero. "); 139 139 else 140 140 { 141 141 printf ("It takes %d seconds to reach the target position, let me show you the way. ",mintime[0][0]); 142 142 point s = arr[I-1]; 143 143 while (s.x != 0 || s.y !=0) 144 144 s = arr[--I]; 145 145 int p ,time = 1 ,x ,y; 146 146 //打印路径 147 147 while (s.front >= 0) 148 148 { 149 149 p = s.front; 150 150 x = arr[p].x ,y = arr[p].y; 151 151 printf ("%ds:(%d,%d)->(%d,%d) ",time++,s.x,s.y,x,y); 152 152 if (map[x][y] != 'X' && map[x][y] != '.') 153 153 for(int i = 1 ;i <= map[x][y]-'0';i++) 154 154 printf("%ds:FIGHT AT (%d,%d) ",time++,x,y); 155 155 s = arr[p]; 156 156 } 157 157 } 158 158 printf ("FINISH "); 159 159 } 160 160 return 0; 161 161 }