zoukankan      html  css  js  c++  java
  • hdoj 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): 11167    Accepted Submission(s): 3411
    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<algorithm>
     3 #include<cstring>
     4 #include<queue>
     5 #include<cstdio>
     6 #include<stack>
     7 using namespace std;
     8 char map[101][101];
     9 int vis[101][101],n,m,mins;
    10 bool rescue;
    11 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    12 struct node
    13 {
    14     int x,y,cnt;
    15     node(int xx=0,int yy=0, int ccnt=0) : x(xx),y(yy),cnt(ccnt){};//不知道什么意思
    16     friend  bool operator <(const node &a,const node &b)
    17     {
    18         return a.cnt>b.cnt;
    19     }
    20 }f[101][101];;
    21 void bfs()
    22 {
    23     node t;
    24     t.x=1;t.y=1;t.cnt=0;
    25     priority_queue< node >Q;
    26     Q.push(t);
    27     vis[1][1]=1;
    28     while(!Q.empty())
    29     {
    30         node b=Q.top();
    31         Q.pop();
    32         if(b.x==n && b.y==m)
    33         {
    34             rescue=1; mins=b.cnt; return ;
    35         }
    36         for(int k=0; k<4 ; k++)
    37         {
    38             int i = b.x+dir[k][0];
    39             int j = b.y+dir[k][1];
    40             if(i<=n && i>0 && j<=m && j>0 && !vis[i][j] && map[i][j]!='X')
    41                {
    42                    //cout<<"***"<<endl;
    43                     vis[i][j]=1;
    44                     f[i][j].x=b.x ; f[i][j].y=b.y; f[i][j].cnt=b.cnt+1;//保存前一个位置
    45                     if(map[i][j]=='.')
    46                         Q.push(node(i,j,b.cnt+1));
    47                     else Q.push(node(i,j,b.cnt+(map[i][j]-'0')+1));
    48                }
    49        }
    50    }
    51 }
    52 void print()
    53 {
    54     stack < node > S;
    55     node temp = f[n][m];
    56     S.push(node(n,m,mins));
    57     while(temp.x!=1 || temp.y!=1)//提取出,所有的位置;
    58     {
    59         S.push(temp);
    60         temp=f[temp.x][temp.y];
    61     }
    62     int t=1;
    63     while(!S.empty())
    64     {
    65         temp=S.top();
    66         S.pop();
    67         if(map[temp.x][temp.y]=='.')
    68              printf("%ds:(%d,%d)->(%d,%d)
    ",t++,f[temp.x][temp.y].x-1,f[temp.x][temp.y].y-1,temp.x-1,temp.y-1);
    69         else {
    70             printf("%ds:(%d,%d)->(%d,%d)
    ",t++,f[temp.x][temp.y].x-1,f[temp.x][temp.y].y-1,temp.x-1,temp.y-1);
    71             int k=map[temp.x][temp.y]-'0';
    72             while(k--)
    73                 printf("%ds:FIGHT AT (%d,%d)
    ",t++,temp.x-1,temp.y-1);
    74         }
    75     }
    76     printf("FINISH
    ");
    77     return ;
    78 }
    79 int main()
    80 {
    81     while(cin>>n>>m)
    82     {
    83         for(int i=1; i<=n; i++)
    84             for(int j=1;j<=m;j++)
    85                cin>>map[i][j];
    86         memset(vis,0,sizeof(vis));
    87         rescue=0;
    88         bfs();
    89          //        cout<<rescue<<endl;
    90         if(rescue)  printf("It takes %d seconds to reach the target position, let me show you the way.
    ",mins);
    91             else  {printf("God please help our poor hero.
    FINISH
    ");continue;}
    92                  print();
    93 
    94     }
    95 }
  • 相关阅读:
    Linux 7 web服务基础知识
    Linux 6 Nginx
    Linux 5 MySQL、redis相关
    Linux 4 安装相关程序
    phpcms 路由配置
    ecmall 入口文件解析 引入了什么
    php 调用天气接口
    phpcms 加载微信类库,生成签名
    ecmall 学习记录2
    Jquery 遍历
  • 原文地址:https://www.cnblogs.com/lovychen/p/3666039.html
Copyright © 2011-2022 走看看