zoukankan      html  css  js  c++  java
  • HDU

    Ignatius and the Princess I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    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 1175 1180 1253 1242 
     
     
     
     
    题解:bfs+优先队列+记录路径
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 #include<string>
     5 using namespace std;
     6 struct que
     7 {
     8     int x,y,s;
     9     string p;
    10     friend bool operator<(const que & a,const que & b)
    11     {
    12         return a.s > b.s;
    13     }
    14 }frt,tmp;
    15 priority_queue<que>Q;
    16 const int dx[]={-1, 0,+1, 0};
    17 const int dy[]={ 0,+1, 0,-1};
    18 int n,m;
    19 char ma[111][111];
    20 char ch[20];
    21 void prt(que q)
    22 {
    23     printf("It takes %d seconds to reach the target position, let me show you the way.
    ",q.s);
    24     int len=q.p.length(),i,j=0,k,t=0;
    25     que pre,now;
    26     pre.x=pre.y=pre.s=0;
    27     for(i=0;i<len;i++)
    28     if(q.p[i]=='-')
    29     {
    30         for(k=0;j<i;j++)ch[k++]=q.p[j];
    31         ch[k]='';
    32         sscanf(ch,"%d,%d,%d",&now.x,&now.y,&now.s);
    33         printf("%ds:(%d,%d)->(%d,%d)
    ",++t,pre.x,pre.y,now.x,now.y);
    34         for(k=now.s-pre.s;k>1;k--)printf("%ds:FIGHT AT (%d,%d)
    ",++t,now.x,now.y);
    35         pre=now;
    36         i=j++;
    37     }
    38     //printf("%s
    ",q.p.c_str());
    39     puts("FINISH");
    40 }
    41 void bfs()
    42 {
    43     while(!Q.empty())Q.pop();
    44     tmp.x=tmp.y=tmp.s=0;
    45     tmp.p="";
    46     Q.push(tmp);
    47     ma[0][0]='X';
    48     int i;
    49     while(!Q.empty())
    50     {
    51         frt=Q.top();
    52         if(frt.x==n-1&&frt.y==m-1)
    53         {
    54             prt(frt);
    55             return;
    56         }
    57         Q.pop();
    58         for(i=0;i<4;i++)
    59         {
    60             tmp.x=frt.x+dx[i];
    61             tmp.y=frt.y+dy[i];
    62             if(0<=tmp.x&&tmp.x<n&&0<=tmp.y&&tmp.y<m&&ma[tmp.x][tmp.y]!='X')
    63             {
    64                 tmp.s=frt.s+1;
    65                 if(ma[tmp.x][tmp.y]!='.')tmp.s+=(ma[tmp.x][tmp.y]-'0');
    66                 sprintf(ch,"%d,%d,%d-",tmp.x,tmp.y,tmp.s);
    67                 tmp.p=frt.p+string(ch);
    68                 Q.push(tmp);
    69                 ma[tmp.x][tmp.y]='X';
    70             }
    71         }
    72     }
    73     printf("God please help our poor hero.
    FINISH
    ");
    74 }
    75 int main()
    76 {
    77     int i;
    78     while(scanf("%d%d",&n,&m)!=EOF)
    79     {
    80         for(i=0;i<n;i++)scanf("%s",ma[i]);
    81         bfs();
    82     }
    83     return 0;
    84 }
     
  • 相关阅读:
    matlab关闭文件
    matlab字符串比较
    matlab画直线
    已解决:TeamViewer使用的设备数量上限
    ubuntu安装teamviewer,缺少依赖处理
    木心的话
    SQL 语句中 where 条件后 写上1=1 是什么意思
    NetCore获取当前请求URL的方法
    NetCore3.1 日志组件 Nlog的使用
    Mysql并发时经典常见的死锁原因及解决方法
  • 原文地址:https://www.cnblogs.com/gangduo-shangjinlieren/p/4990429.html
Copyright © 2011-2022 走看看