• 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): 19789    Accepted Submission(s): 6446
    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
     
     
    题解:记录路劲的走迷宫问题
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <string.h>
      4 using namespace std;
      5 const int MAX =100+10;
      6 #include <queue>
      7 bool used[MAX][MAX];//标记该坐标是否呗更新过
      8 int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//方向坐标
      9 #include <stack>
     10 struct node//
     11 {
     12     int i;
     13     int j;//更新的坐标
     14     char ch;//当前坐标的字符
     15     int sum;//走到当前坐标所需要花费的最小时间
     16     friend bool operator<(node a,node b)//优先队列
     17     {
     18         return a.sum>b.sum;
     19     }
     20 };
     21 struct nodd//
     22 {
     23     int i;
     24     int j;//记录起父亲节点的值
     25     int sum;//走到当前点的值
     26     int x;
     27     int y;//当前点
     28 };
     29 int n,m;
     30 node a[MAX][MAX];
     31 void bfs()
     32 {
     33     priority_queue<node>q;
     34     stack<nodd>s;
     35     node temp;
     36     memset(used,false,sizeof(used));
     37     temp.i=0;
     38     temp.j=0;
     39     temp.sum=0;
     40     used[temp.i][temp.j]=true;
     41     q.push(temp);
     42     while(!q.empty())
     43     {
     44         node flag=q.top();
     45         q.pop();
     46         for(int i=0;i<4;i++)//往四个方向搜
     47         {
     48             node type=flag;
     49             type.i+=dir[i][0];
     50             type.j+=dir[i][1];
     51             if(type.i>=0&&type.i<n&&type.j>=0&&type.j<m&&a[type.i][type.j].ch!='X'&&!used[type.i][type.j])//判断是否满足要求
     52             {
     53                 a[type.i][type.j].i=flag.i;
     54                 a[type.i][type.j].j=flag.j;//记录父亲节点
     55                 if(a[type.i][type.j].ch>'0'&&a[type.i][type.j].ch<='9')//遇到小怪,打小怪花费的时间
     56                 {
     57                     type.sum+=a[type.i][type.j].ch-'0';
     58                 }
     59                 used[type.i][type.j]=true;
     60                 ++type.sum;
     61                 if(type.i==n-1&&type.j==m-1)//判断是否找到
     62                 {
     63                     int k=type.sum;
     64                     nodd typ;
     65                     typ.i=type.i;
     66                     typ.j=type.j;
     67                     for(int j=k;j>=1;j--)
     68                     {
     69                         typ.x=typ.i;
     70                         typ.y=typ.j;
     71                         if(a[typ.i][typ.j].ch>'0'&&a[typ.i][typ.j].ch<='9')//该点是否为小怪出现的点
     72                         {
     73                             int x;
     74                             nodd ty;
     75                             for(x=j;x>j-a[typ.i][typ.j].ch+'0';x--)
     76                             {
     77                                 ty.i=-1;
     78                                 ty.x=typ.i;
     79                                 ty.y=typ.j;
     80                                 ty.sum=x;
     81                                 s.push(ty);
     82                             }
     83                             j=x;
     84                         }
     85                         typ.i=a[typ.x][typ.y].i;
     86                         typ.j=a[typ.x][typ.y].j;
     87                         typ.sum=j;
     88                         s.push(typ);
     89 
     90                     }
     91                     printf("It takes %d seconds to reach the target position, let me show you the way.
    ",type.sum);
     92                     while(!s.empty())
     93                     {
     94                         if(s.top().i!=-1)
     95                         {
     96                             printf("%ds:(%d,%d)->(%d,%d)
    ",s.top().sum,s.top().i,s.top().j,s.top().x,s.top().y);
     97                         }
     98                         else printf("%ds:FIGHT AT (%d,%d)
    ",s.top().sum,s.top().x,s.top().y);
     99                         s.pop();
    100                     }
    101                     printf("FINISH
    ");
    102                     return;
    103                 }
    104                 q.push(type);
    105             }
    106         }
    107     }
    108     printf("God please help our poor hero.
    FINISH
    ");
    109     return;
    110 }
    111 int main()
    112 {
    113     while(scanf("%d%d",&n,&m)!=EOF)
    114     {
    115         getchar();
    116         memset(used,false,sizeof(used));
    117         for(int i=0;i<n;i++)
    118         {
    119             for(int j=0;j<m;j++)
    120             {
    121                 scanf("%c",&a[i][j].ch);
    122             }
    123             getchar();
    124         }
    125         bfs();
    126     }
    127     return 0;
    128 }
    View Code
  • 相关阅读:
    ios awakeFromNib 和 initWithCoder:
    iOS 关于iphone6 和 iphone6 plus 的适配
    iOS 目录的使用
    iOS 8 WKWebView 知识点
    iOS 动画结束后 view的位置 待完善
    iOS coredata 数据库升级 时报Can't find model for source store
    iOS 真机文件系统区分大小写,而模拟器可能不区分
    iOS coredata 级联删除
    iOS 关于AFNetworking ssl 待完成
    iOS 关于UIWindow的理解
  • 原文地址:https://www.cnblogs.com/52why/p/7482917.html
Copyright © 2020-2023  润新知