Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18847    Accepted Submission(s): 6090
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
 /*
     Name: hdu--1026--Ignatius and the Princess I
     Copyright: ©2017 日天大帝
     Author: 日天大帝
     Date: 21/04/17 17:30
     Description: bfs搜索路径,dfs打印路径
 */
 #include<cstring>
 #include<queue>
 #include<iostream>
 #include<cstdio>
 #include<cstdlib>
 using namespace std;
 struct node{
     int x,y,steps;
     bool operator<(const node&a)const{
         return steps>a.steps;
     }
 };
 int bfs();
 void dfs(int,int);
 ;
 int map[MAX][MAX];
 int to[MAX][MAX];
 int cnt[MAX][MAX];
 int n,m,ct;
 ][] = {{,},{,-},{,},{-,}};
 int main(){

 //    freopen("in.txt","r",stdin);
     ios::sync_with_stdio(false);

     while(cin>>n>>m){
         memset(map,,sizeof(map));
         memset(to,,sizeof(to));
         memset(cnt,,sizeof(cnt));
         ; i<n; ++i){
             ; j<m; ++j){
                 char ch;cin>>ch;
                 if(ch == '.'){
                     map[i][j] = ;
                 };
                 ';
             }
         }
         int ans = bfs();
         if(ans){
             cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl;
             ct = ;
             dfs(n-,m-);
         }else printf("God please help our poor hero.\n");
         cout<<"FINISH\n";//G++WA无数次,hdu C++ AC了
     }
     ;
 }
 void dfs(int x,int y){
     if(!to[x][y])return ;
     int i,j;
     i = x - dir[to[x][y]-][];//剪枝
     j = y - dir[to[x][y]-][];//剪枝
     dfs(i,j);
     printf("%ds:(%d,%d)->(%d,%d)\n",ct++,i,j,x,y);
     while(cnt[x][y]--) {
          printf("%ds:FIGHT AT (%d,%d)\n",ct++,x,y);
     }
 }
 int bfs(){
     node start;
     start.x = ;
     start.y = ;
     start.steps = ;
     map[][] = -;
     priority_queue<node> q;
     q.push(start);
     while(!q.empty()){
         node a,temp = q.top();q.pop();
          && temp.y == m-)return temp.steps;
         ; i<; ++i){//i<4(n)
             a.x = temp.x + dir[i][];
             a.y = temp.y + dir[i][];
              ||a.y< || map[a.x][a.y] == -)continue;
             a.steps = temp.steps + map[a.x][a.y] + ;
             map[a.x][a.y] = -;
             to[a.x][a.y] = i+;//剪枝
             q.push(a);
         }
     }
     ;
 }
  

hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))的更多相关文章

  1. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  2. hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...

  3. 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 (J ...

  4. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  5. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  6. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  7. hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. 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 (J ...

  9. hdu 1026 Ignatius and the Princess I 搜索,输出路径

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  10. HDU 1026 Ignatius and the Princess I(带路径的BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...

随机推荐

  1. Linux下Shadow socks的安装和配置

    实在受不了在Windows下编程,所以自己就安装了一个Ubutun,公司用的FQ软件shadowsocks在Windows上用起来很简单很爽,但是在Ubutun上的安装和配置就没那么简单了,写下这篇文 ...

  2. 基于TFS的.net技术路线的云平台DevOps实践

    DevOps是近几年非常流行的系统研发管理模式,很多公司都或多或少在践行DevOps.那么,今天就说说特来电云平台在DevOps方面的实践吧. 说DevOps,不得不说DevOps的具体含义.那么,D ...

  3. PHP htmlspecialchars和htmlspecialchars_decode(函数)

    htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 函数原型:htmlspecialchars(string,quotestyle,character-set) 预定 ...

  4. Visiual Studio CLR20r3

    问题事件名称: CLR20r3     解决方法:   步骤1:开始-->所有程序-->Microsoft Visual Studio 2012-->Visual Studio To ...

  5. Selective Search for Object Recognition 论文笔记【图片目标分割】

    这篇笔记,仅仅是对选择性算法介绍一下原理性知识,不对公式进行推倒. 前言: 这篇论文介绍的是,如果快速的找到的可能是物体目标的区域,不像使用传统的滑动窗口来暴力进行区域识别.这里是使用算法从多个维度对 ...

  6. Vijos 1981 跳石头 二分

    描述 一年一度的"跳石头"比赛又要开始了! 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N 块岩 ...

  7. Scratch2的离线下载与安装

    scratch是一种程序设计语言,可以用来设计 故事.动画.游戏.音乐和美术作品. Scratch主页:https://scratch.mit.edu/ Scratch的下载与安装: 首先下载并安装A ...

  8. 用VS Code体验调试.NET Core 2.0 Preview (传统三层架构)

    准备工作 VS Code下载地址:https://vscode.cdn.azure.cn/stable/379d2efb5539b09112c793d3d9a413017d736f89/VSCodeS ...

  9. Kubernetes服务之“运行单实例的有状态服务”

    目标 在你的环境中创建一个PV 创建一个MySQl的Deployment 在集群中以DNS名称的方式,将MySQL暴露给其他的pod 开始之前 你需要一个Kubernetes集群,一个可以连接到集群的 ...

  10. Spring MVC 表单验证

    1. 基于 JSR-303(一个数据验证的规范): import javax.validation.constraints.Min; import javax.validation.constrain ...