zoukankan      html  css  js  c++  java
  • 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): 20082    Accepted Submission(s): 6542
    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
     
     1 #include<iostream>
     2 #include<fstream>
     3 #include<queue>
     4 #include<string>
     5 using namespace std;
     6 const int MAX=105;
     7 struct pot
     8 {
     9     int x;
    10     int y;
    11 };
    12 struct data
    13 {
    14     pot pre;
    15     int zhi;
    16     int times;
    17 };
    18 data arr[MAX][MAX];
    19 void showR(int n,int m)
    20 {
    21     int i;
    22     if(n==1&&m==1)
    23     {
    24         for(i=1;i<=arr[n][m].zhi;i++)
    25             cout<<i<<"s:FIGHT AT ("<<n-1<<','<<m-1<<')'<<endl;
    26         return;
    27     }
    28     showR(arr[n][m].pre.x,arr[n][m].pre.y);
    29     cout<<arr[n][m].times-arr[n][m].zhi<<"s:("<<arr[n][m].pre.x-1<<','<<arr[n][m].pre.y-1<<")->("<<n-1<<','<<m-1<<')'<<endl;
    30     if(arr[n][m].zhi>0)
    31     {
    32         for(i=arr[n][m].times-arr[n][m].zhi+1;i<=arr[n][m].times;i++)
    33             cout<<i<<"s:FIGHT AT ("<<n-1<<','<<m-1<<')'<<endl;
    34     }
    35 }
    36 int main()
    37 {
    38     //ifstream in("data.txt");
    39     int n,m;
    40     queue<pot> que;
    41     int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    42     while(cin>>n>>m)
    43     {
    44         int i,j;
    45         for(i=1;i<=n;i++)
    46         {
    47             string s;
    48             cin>>s;
    49             for(j=1;j<=m;j++)
    50             {
    51                 arr[i][j].times=0;
    52                 if(s[j-1]=='X')
    53                     arr[i][j].zhi=-1;
    54                 else if(s[j-1]=='.')
    55                     arr[i][j].zhi=0;
    56                 else
    57                     arr[i][j].zhi=s[j-1]-'0';
    58             }
    59         }
    60         for(i=0;i<=n+1;i++)
    61         {
    62             arr[i][0].zhi=-1;
    63             arr[i][m+1].zhi=-1;
    64         }
    65         for(j=0;j<=m+1;j++)
    66         {
    67             arr[0][j].zhi=-1;
    68             arr[n+1][j].zhi=-1;
    69         }
    70         pot temp={1,1};
    71         que.push(temp);
    72         arr[1][1].times=arr[1][1].zhi;
    73         while(!que.empty())
    74         {
    75             pot fro=que.front();
    76             que.pop();
    77             for(i=0;i<4;i++)
    78             {
    79                 pot next={fro.x+dir[i][0],fro.y+dir[i][1]};
    80                 if(arr[next.x][next.y].zhi!=-1)//非障碍
    81                     if(arr[next.x][next.y].times==0||arr[next.x][next.y].times>arr[next.x][next.y].zhi+1+arr[fro.x][fro.y].times)
    82                     {
    83                         arr[next.x][next.y].times=arr[next.x][next.y].zhi+1+arr[fro.x][fro.y].times;
    84                         arr[next.x][next.y].pre=fro;
    85                         que.push(next);
    86                     }
    87             }
    88         }
    89         if(arr[n][m].times==0)
    90             cout<<"God please help our poor hero."<<endl;
    91         else
    92         {
    93             cout<<"It takes "<<arr[n][m].times<<" seconds to reach the target position, let me show you the way."<<endl;
    94             showR(n,m);
    95         }
    96         cout<<"FINISH"<<endl;
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    Microsoft.Office.Interop.Excel 组件的报错检索 COM 类工厂中 CLSID,错误码:80070005
    SQLServer编写自己的切分函数 SPLIT和带排序的切割函数 SPLITSort
    Powerdesigner 导出Excel格式数据字典 导出Excel格式文件
    NPOI 导入导出excel两种方式 和 错误 无法访问已关闭的流解决方式
    unable to start the virtual device;Genymotion启动安卓模拟器出错
    datetimepicker 时间控件 1899年问题以及解决方法
    Linux命令大全
    Linux工具包
    Redis
    MySQL索引
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/7900817.html
Copyright © 2011-2022 走看看