Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17171    Accepted Submission(s): 5495
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
 
题意:有个英雄要从(0,0)->(n-1,m-1),他可以经过 '.' 或者标有数字 1- 9的地方,经过 '.' 需要1s钟,经过数字需要的时间为当前的数字+1,问英雄出去最短的时间并输出路径。
题解:有数字所以用优先队列,输出路径比较麻烦,用while循环或者递归都可以。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int N = ;
struct Node{
int x,y,step,v;
};
bool operator < (Node a,Node b){
return a.step > b.step;
}
struct Per{
int x,y,v;
}cnt[N][N];
int n,m;
bool vis[N][N];
char graph[N][N];
int dir[][] = {{-,},{,},{,-},{,}};
int bfs(){
memset(vis,false,sizeof(vis));
Node s;
vis[][] = true;
s.x = ,s.y = ,s.step = ,s.v = ;
if(graph[][]=='X') return -;
if(graph[][]!='.'){
s.step = s.v = graph[][]-'';
}
priority_queue<Node> q;
q.push(s);
while(!q.empty()){
Node now = q.top();
q.pop();
if(now.x==n-&&now.y==m-) {
cnt[n][m].x = n-;
cnt[n][m].y = m-;
cnt[n][m].v = now.v;
return now.step;
}
for(int i=;i<;i++){
Node next;
next.x = now.x+dir[i][];
next.y = now.y+dir[i][];
if(vis[next.x][next.y]||next.x<||next.x>=n||next.y<||next.y>=m||graph[next.x][next.y]=='X') continue;
char temp = graph[next.x][next.y];
if(temp=='.'){
next.step = now.step+;
next.v = ;
}else{
next.step=now.step+temp-''+;
next.v = temp-'';
}
vis[next.x][next.y] = true;
q.push(next);
cnt[next.x][next.y].x = now.x;
cnt[next.x][next.y].y = now.y;
cnt[next.x][next.y].v = now.v;
}
}
return -;
}
int _count;
void dfs(int x,int y){
if(x==&y==) return;
dfs(cnt[x][y].x,cnt[x][y].y);
int v = cnt[x][y].v;
while(v--){
printf("%ds:FIGHT AT (%d,%d)\n",_count++,cnt[x][y].x,cnt[x][y].y);
}
if(x!=n&&y!=m)
printf("%ds:(%d,%d)->(%d,%d)\n",_count++,cnt[x][y].x,cnt[x][y].y,x,y);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<n;i++){
scanf("%s",graph[i]);
}
int ans = bfs();
if(ans==-){
printf("God please help our poor hero.\n");
}else{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
_count = ;
dfs(n,m);
}
printf("FINISH\n");
}
}

hdu 1026(优先队列+路径输出)的更多相关文章

  1. hdu 1026(BFS+输出路径) 我要和怪兽决斗

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...

  2. 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 ...

  3. [POJ] 1606 Jugs(BFS+路径输出)

    题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...

  4. java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用

    java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用 import java.io.BufferedReader; import java.io.BufferedWrite ...

  5. Floyd最短路(带路径输出)

    摘要(以下内容来自百度) Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似. 该算法名称以创始人之一.1978年图灵奖获得者. ...

  6. 【CH5104】I-country 线性dp+路径输出

    pre:在网格中,凸多边形可以按行(row)分解成若干段连续的区间 [ l , r ] ,且左端点纵坐标的值(col)满足先减后增,右端点纵坐标先增后减. 阶段:根据这个小发现,可以将阶段设置成每一行 ...

  7. URAL 1004 Sightseeing Trip(floyd求最小环+路径输出)

    https://vjudge.net/problem/URAL-1004 题意:求路径最小的环(至少三个点),并且输出路径. 思路: 一开始INF开大了...无限wa,原来相加时会爆int... 路径 ...

  8. 洛谷 P2764 最小路径覆盖问题【最大流+拆点+路径输出】

    题目链接:https://www.luogu.org/problemnew/show/P2764 题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V ...

  9. UVA--624 CD(01背包+路径输出)

    题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. Spring.NET中事务管理【转】

    http://www.cnblogs.com/GoodHelper/archive/2009/11/16/springnet_transaction.html 浏览了下写的比较清楚. 在.NET FC ...

  2. 【逆序对相关/数学】【P1966】【NOIP2013D1T2】 火柴排队

    传送门 Description 涵涵有两盒火柴,每盒装有 $n$ 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为:$ \sum ...

  3. E. Border

    E. Border time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  4. 实现一个简单的Vue插件

    我们先看官方文档对插件的描述 插件通常会为 Vue 添加全局功能.插件的范围没有限制--一般有下面几种: 1.添加全局方法或者属性,如: vue-custom-element 2.添加全局资源:指令/ ...

  5. 数据结构:hash_map

    在C++11以上的开发环境中,请直接把map替换成unordered_map,不要使用hash_map 之前我们专门有一篇介绍哈希表,多维哈希表的博文,当时就是以map举例子,然后说了一句把map替换 ...

  6. [Luogu 2221] HAOI2012 高速公路

    [Luogu 2221] HAOI2012 高速公路 比较容易看出的线段树题目. 由于等概率,期望便转化为 子集元素和/子集个数. 每一段l..r中,子集元素和为: \(\sum w_{i}(i-l+ ...

  7. [BZOJ2440]完全平方数解题报告|莫比乌斯函数的应用

    完全平方数 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱.  这天是小X的生日 ...

  8. 【BZOJ】1552/3506 [Cerc2007]robotic sort

    [算法]splay [题解] splay维护序列,用权值(离散化)作为编号.每次找第i小的话直接找对应编号splay即可. 但是这样splay没有下传翻转标记?直接暴力找到路径然后从根到改结点push ...

  9. GitLab 迁移与升级

    参考: [ 博客园 BigBao ] 环境说明: OS: CentOS 7.x gitlab-ce 初始版本: 8.8.5 gitlab-ce 升级到版本: 11.2.3 升级方式: rpm 安装升级 ...

  10. java 连接MySQL的代码

    1.java connect MySQL as conding. https://www.cnblogs.com/centor/p/6142775.html