时间: 2020-11-23|43次围观|0 条评论

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13911    Accepted Submission(s): 4370 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.  

 

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) 14s:FIGHT AT (4,5) FINISH God please help our poor hero. FINISH
hdu1026.Ignatius and the Princess I(bfs + 优先队列)插图
hdu1026.Ignatius and the Princess I(bfs + 优先队列)插图1

 1 #include<queue>
 2 #include<algorithm>
 3 #include<stdio.h>
 4 #include<string.h>
 5 #include<ctype.h>
 6 const int M = 100 + 10 , inf = 0x3f3f3f3f ;
 7 int n , m;
 8 char map[M][M] ;
 9 bool vis[M][M] ;
10 int move[][2] = {
      {
      1,0} , {-1,0} , {
      0,1} , {
      0,-1}} ;
11 struct node
12 {
13     int  x , y , time ;
14     int pet ;
15     int nxt ;
16 }e[90000 + 10];
17 int l , r ;
18 int cas ,k ;
19 
20 bool cmp (node a , node b)
21 {
22     return a.time < b.time ;
23 }
24 void bfs ()
25 {
26     node ans , tmp ;
27     l = 0 , r = 1 ;
28     k = 0 ;
29     for (int i = 0 ; i < n ; i ++) for (int j = 0 ; j < m ; j ++) vis[i][j] = 0;
30     e[l].x = 0 , e[l].y = 0 , e[l].time = 0 , e[l].nxt = - 1 , e[l].pet = 0 ;
31     while (l != r) {
32         std::sort (e + l , e + r , cmp ) ;
33         //printf ("(%d,%d)\n" , e[l].x , e[l].y ) ;
34         ans = e[l]  ;
35         if (e[l].x == n - 1 && e[l].y == m - 1) {k = 1 ; return ;}
36        // printf ("S====(%d,%d)\n" , ans.x , ans.y ) ;
37         for (int i = 0 ; i < 4 ; i ++) {
38             tmp.x = ans.x + move[i][0] , tmp.y = ans.y + move[i][1] ; tmp.pet = 0 ;
39             if (tmp.x < 0 || tmp.y < 0 || tmp.x >= n || tmp.y >= m) continue ;
40             if (map[tmp.x][tmp.y] == 'X' ) continue ;
41             if (vis[tmp.x][tmp.y]) continue ;
42             if ( isdigit ( map[tmp.x][tmp.y] ) )  {tmp.time = ans.time + 1 + map[tmp.x][tmp.y] - '0' ; tmp.pet = map[tmp.x][tmp.y] - '0' ;}
43             else tmp.time = ans.time + 1 ;
44             tmp.nxt = l ;
45             vis[tmp.x][tmp.y] = 1 ;
46          //    printf ("(%d,%d)\n" , tmp.x , tmp.y ) ;
47             e[r ++] = tmp ;
48         }
49         l ++ ;
50     }
51 }
52 
53 void solve (int k)
54 {
55     if (k == -1) return ;
56     solve (e[k].nxt) ;
57     int t = e[k].nxt ;
58     if ( t != -1 ) {
59         printf ("%ds:(%d,%d)->(%d,%d)\n"  , cas ++ , e[t].x , e[t].y , e[k].x , e[k].y ) ;
60         for (int i = 0 ; i < e[k].pet ; i ++) printf ("%ds:FIGHT AT (%d,%d)\n" , cas ++ , e[k].x , e[k].y) ;
61     }
62 }
63 
64 int main ()
65 {
66   // freopen ("a.txt" , "r" , stdin ) ;
67     while (~ scanf ("%d%d" , &n , &m)) {
68         for (int i = 0 ; i < n ; i ++) scanf ("%s" , map[i]) ;
69         bfs () ;
70         if (k) {
71             printf ("It takes %d seconds to reach the target position, let me show you the way.\n" , e[l].time ) ;
72             cas = 1 ;
73             solve (l) ;
74             puts ("FINISH") ;
75         }
76         else {
77             puts ("God please help our poor hero.") ;
78             puts ("FINISH") ;
79         }
80     }
81     return 0 ;
82 }

View Code

这种不需要走回头路的,而且走的过程中会出现罚时的题目,就用优先队列吧.
发现输出路径的用模拟来写很方便.

转载于:https://www.cnblogs.com/get-an-AC-everyday/p/4468648.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/98131220

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《hdu1026.Ignatius and the Princess I(bfs + 优先队列)
   

还没有人抢沙发呢~