zoukankan      html  css  js  c++  java
  • hdu 1026 Ignatius and the Princess I(bfs)

    Ignatius and the Princess I

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

     
    Author
    Ignatius.L
     
    Recommend
    We have carefully selected several similar problems for you:  1072 1180 1372 1043 1254 
     
    这道题的题目特别长,同样要输出走过的路径,采用了优先队列和以前写过的方法输出,测试数据过了,但提交超时,所以查找了网上大神的代码,优先队列并不会超时,而应该是我的输出方法有问题,于是使用了深搜的方法输出,感觉还是很方便的。
     
    题意:骑士(地图左上角)要去救公主(地图右下角),而他们之间(地图上)有3种物体,‘.’为路,可以通过;‘X’为墙,不能通过;'1'~'9'数字为怪物的血量num。骑士遇到怪物,要停留num秒,求骑士能否到达公主的位置,如果能到达,输出最短路径的长度,并输出路径,如果不能到,输出特定语句。
     
    附上代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 
     7 struct node
     8 {
     9     int x,y,step;
    10     friend bool operator < (node n1,node n2)  //优先队列,队列从小到大排序
    11     {
    12         return n1.step>n2.step;
    13     }
    14 } s1,s2,ss[110][110]; //ss数组记录走的所有路径
    15 char map[105][105];
    16 int visit[105][105],n,m,t,p;
    17 int f[4][2]= {0,1,0,-1,1,0,-1,0};
    18 
    19 void BFS()
    20 {
    21     priority_queue <node> q;  //优先队列的定义
    22     while(!q.empty())
    23         q.pop();
    24     s1.x=0;
    25     s1.y=0;
    26     s1.step=0;
    27     visit[0][0]=1;
    28     ss[s1.x][s1.y].x=0;    //起点为左上角
    29     ss[s1.x][s1.y].y=0;
    30     q.push(s1);
    31     while(!q.empty())
    32     {
    33         s1=q.top();
    34         q.pop();
    35         if(s1.x==n-1&&s1.y==m-1)   //终点为右下角
    36         {
    37             p=s1.step;
    38             return;
    39         }
    40         for(int i=0; i<4; i++)
    41         {
    42             s2=s1;
    43             s2.x=s1.x+f[i][0];
    44             s2.y=s1.y+f[i][1];
    45             if(s2.x>=0&&s2.x<n&&s2.y>=0&&s2.y<m&&map[s2.x][s2.y]!='X'&&!visit[s2.x][s2.y])
    46             {
    47                 visit[s2.x][s2.y]=1;
    48                 s2.step=s1.step+1;
    49                 if(map[s2.x][s2.y]>='1'&&map[s2.x][s2.y]<='9')   //遇到怪物的时候需要耗得时间
    50                     s2.step=s2.step+map[s2.x][s2.y]-'0';
    51                 ss[s2.x][s2.y].x=s1.x;     //记录走到这一步的上一步坐标
    52                 ss[s2.x][s2.y].y=s1.y;
    53                 q.push(s2);
    54             }
    55         }
    56     }
    57     p=-1;    //到不了的标记
    58     return;
    59 }
    60 
    61 void print(int x,int y)  //深搜输出,从终点开始往前搜
    62 {
    63     if(x==0&&y==0) return;     //找到起点,返回上一次
    64     print(ss[x][y].x,ss[x][y].y);
    65     printf("%ds:(%d,%d)->(%d,%d)
    ",t++,ss[x][y].x,ss[x][y].y,x,y);
    66     if(map[x][y]>='1'&&map[x][y]<='9')  //如果遇到怪兽,多呆几秒的输出
    67     {
    68         int w=map[x][y]-'0';
    69         for(int i=w; i>0; i--)
    70             printf("%ds:FIGHT AT (%d,%d)
    ",t++,x,y);
    71     }
    72 }
    73 int main()
    74 {
    75     int i,j;
    76     while(~scanf("%d%d",&n,&m))
    77     {
    78         for(i=0; i<n; i++)
    79             scanf("%s",&map[i]);
    80         memset(visit,0,sizeof(visit));
    81         BFS();
    82         t=1;
    83         if(p==-1)    //到不了的特定输出
    84             printf("God please help our poor hero.
    ");
    85         else
    86         {
    87             printf("It takes %d seconds to reach the target position, let me show you the way.
    ",p); //输出一共需要多少秒
    88             print(n-1,m-1);
    89         }
    90         printf("FINISH
    ");    //别忘了需要输出的最后一句话
    91     }
    92     return 0;
    93 }
     
  • 相关阅读:
    CMY/CMYK 打印机色彩
    safe RGB colors
    人眼内的三类视锥细胞
    函数极限的定义
    Region的周长, 面积与紧凑程度
    Boundary Representations
    Boundary Following Algorithm
    Region Representaion and Description
    JavaSE编码试题强化练习5
    JavaSE编码试题强化练习4
  • 原文地址:https://www.cnblogs.com/pshw/p/4760048.html
Copyright © 2011-2022 走看看