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): 9934    Accepted Submission(s): 2972
    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.

    题意:从(0,0)点到地图最右下角的最少步数,并把路径输出。使用优先队列求少步数+路径记录数组father[]标记。

      1 #include<iostream>
      2 #include<cstring>
      3 #include<vector>
      4 #include<cstdio>
      5 #include<queue>
      6 #include<stack>
      7 #include<conio.h>
      8 using namespace std;
      9 stack<int> s;
     10 int vis[103][104];
     11 char map[102][102];
     12 int father[10003];
     13 int Row,Column,Char_Num;
     14 int Dir[][2] = {-1,0,0,1,1,0,0,-1};
     15 struct Point
     16 {
     17     int x,y;
     18     int time;
     19     bool operator < (const Point &a)const{
     20         return a.time < time;
     21     }
     22 };
     23 int Time(char c)
     24 {
     25     if( c<='9' && c>='0')return c-'0';
     26     return 0;
     27 }
     28 bool Check(int x,int y)
     29 {
     30     if( x<0 || x>=Row || y<0 || y>=Column || map[x][y] == 'X' || vis[x][y])
     31         return 0;
     32     return 1;
     33 }
     34 void Prin(int T)
     35 {
     36     int i,n,t=1,N = Row * Column - 1;
     37     while( N != 0){
     38         s.push(N);//入栈
     39         if( (n = Time(map[N/Column][N%Column])) )
     40             for(i=1;i<=n;i++)
     41                 s.push(N);//该点是数字,则入栈相应次数
     42         N = father[N];//上一个节点
     43     }
     44     printf("It takes %d seconds to reach the target position, let me show you the way.
    ",T);
     45     N = s.top(); s.pop();
     46     printf("%ds:(0,0)->(%d,%d)
    ",t++,N/Column,N%Column);
     47     while( !s.empty())
     48     {
     49         if(N == s.top())
     50             printf("%ds:FIGHT AT (%d,%d)
    ",t++,N/Column,N%Column);
     51         else
     52             printf("%ds:(%d,%d)->(%d,%d)
    ",t++,N/Column,N%Column,s.top()/Column,s.top()%Column);
     53         N = s.top();
     54         s.pop();
     55     }
     56 }
     57 int BFS(int x,int y)
     58 {
     59     int T;
     60     Point Now_Q,Next_P;
     61     priority_queue<Point> Que;
     62     Next_P.x = x; Next_P.y = y; Next_P.time = 0;
     63     vis[x][y] = 1;
     64     Que.push(Next_P);
     65     while( !Que.empty() )
     66     {
     67         Now_Q = Que.top();
     68         Que.pop();
     69         if(Now_Q.x == Row-1 && Now_Q.y == Column-1){//找到输出
     70             Prin(Now_Q.time);
     71             return 1;
     72         }
     73         for(int i=0;i<4;i++){
     74             Next_P.x = Now_Q.x + Dir[i][0];
     75             Next_P.y = Now_Q.y + Dir[i][1];
     76             if( Check(Next_P.x, Next_P.y)){
     77                 father[Next_P.x*Column + Next_P.y ] = Now_Q.x*Column + Now_Q.y;//标记当前节点的上一个节点
     78                 Next_P.time = Now_Q.time + 1;
     79                 if( (T = Time(map[Next_P.x][Next_P.y])) ){//该点是数字,时间增加相应数字
     80                     Next_P.time += T;
     81                 }
     82                 Que.push(Next_P);
     83                 vis[Next_P.x][Next_P.y] = 1;
     84             }
     85         }
     86     }
     87     return 0;
     88 }
     89 int main()
     90 {
     91     int i,j;
     92     while(~scanf("%d%d",&Row,&Column))
     93     {
     94         getchar();
     95         for(i=0;i<Row;i++)
     96             for(j=0;j<Column;j++){
     97                 cin>>map[i][j];
     98             }
     99         memset(vis,0,sizeof(vis));
    100         memset(father,0,sizeof(father));
    101         while( !s.empty())s.pop();
    102         if( !BFS(0,0) )
    103             printf("God please help our poor hero.
    ");
    104         puts("FINISH");
    105     }
    106     return 0;
    107 }
    View Code
  • 相关阅读:
    8月的list
    hdu 2853
    【问题交流】JAVA 基础 switch 执行顺序的问题
    h5/css动态旋转木马源码
    javascript系列丛书之读后感
    java运行闪退,报错如下,是因为ole32.dll的问题吗?
    js
    切换为文本框编辑状态,点击空白区域保存修改
    前端工程师源码分享:html5 2d 扇子
    2017武汉马拉松4月9日开跑啦~~~
  • 原文地址:https://www.cnblogs.com/qiu520/p/3253112.html
Copyright © 2011-2022 走看看