题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1026

Ignatius and the Princess I

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

bfs+路径记录。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int Max_N = ;
const int Mod = ;
typedef unsigned long long ull;
const int dx[] = { , , -, }, dy[] = { -, , , };
bool vis[Max_N][Max_N];
char maze[Max_N][Max_N];
int N, M, res, fa[Max_N][Max_N], dir[Max_N][Max_N];
struct Node {
int x, y, s;
Node(int i = , int j = , int k = ) :x(i), y(j), s(k) {}
inline bool operator<(const Node &a) const {
return s > a.s;
}
};
void bfs() {
cls(vis, false), cls(fa, ), cls(dir, );
priority_queue<Node> q;
q.push(Node());
while (!q.empty()) {
Node t = q.top(); q.pop();
if (t.x == N - && t.y == M - ) { res = t.s; return; }
rep(i, ) {
int nx = t.x + dx[i], ny = t.y + dy[i];
char &ch = maze[nx][ny];
if (nx < || nx >= N || ny < || ny >= M) continue;
if (ch == 'X' || vis[nx][ny]) continue;
if ('' <= ch && ch <= '') q.push(Node(nx, ny, t.s + + ch - ''));
else q.push(Node(nx, ny, t.s + ));
vis[nx][ny] = true;
fa[nx][ny] = t.x * Mod + t.y;
dir[nx][ny] = i;
}
}
res = -;
}
void show_path(int x, int y) {
if (- == res) { printf("God please help our poor hero.\nFINISH\n"); return; }
int fx, fy;
vector<int> path;
for (;;) {
fx = fa[x][y] / Mod;
fy = fa[x][y] % Mod;
if (!x && !y) break;
path.push_back(dir[x][y]);
x = fx, y = fy;
}
printf("It takes %d seconds to reach the target position, let me show you the way.\n", res);
fx = fy = ;
for (int i = sz(path) - , j = ; ~i && j <= res; i--) {
x = fx + dx[path[i]], y = fy + dy[path[i]];
char &ch = maze[x][y];
if ('' <= ch && ch <= '') {
int t = ch - '';
printf("%ds:(%d, %d)->(%d, %d)\n", j++, fx, fy, x, y);
while (t--) printf("%ds:FIGHT AT (%d,%d)\n", j++, x, y);
}
else printf("%ds:(%d, %d)->(%d, %d)\n", j++, fx, fy, x, y);
fx = x, fy = y;
}
puts("FINISH");
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (~scanf("%d %d", &N, &M)) {
rep(i, N) scanf("%s", maze[i]);
bfs();
show_path(N - , M - );
}
return ;
}

hdu 1026 Ignatius and the Princess I的更多相关文章

  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】

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

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

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

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

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

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

  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(带路径的BFS)

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

随机推荐

  1. mysql中字符集和校对规则

    首先,明确一下字符集和校对规则的概念:    字符集(charset):是一套符号和编码    校对规则(collation):是在字符集内用于比较字符的一套规则,比如有的规则区分大小写,有的则无视 ...

  2. Android之EventBus使用详解

    一.概述 当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,例如:当某一条件发生时,应用中有几个部件对这个消息感兴趣,那么我们通常采用的就是观察者模式,使用观察者模式有一个 ...

  3. 洛谷P2723 丑数 Humble Numbers

    P2723 丑数 Humble Numbers 52通过 138提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 对于一给定的素数 ...

  4. MongoDB 2: 安装和使用

    导读:上篇博客中简单介绍了MongoDB,本篇文章主要是介绍Mongo的安装和使用(环境为win8).(PS:这是一篇没什么技术含量的文章,仅是个人的笔记式文档)下一篇博客,将介绍Mongo使用过程中 ...

  5. C#委托零基础理解

    C#委托零基础理解(转) 1,  为什么使用委托  2.什么是委托  3.委托如何使用 为什么使用委托? 委托是c#中非常重要的一个概念,使用委托使程序员可以将方法引用封装在委托对象内.然后可以将该委 ...

  6. PHP超时处理全面总结(转)

    [ 概述 ] 在PHP开发中工作里非常多使用到超时处理到超时的场合,我说几个场景: 1. 异步获取数据如果某个后端数据源获取不成功则跳过,不影响整个页面展现 2. 为了保证Web服务器不会因为当个页面 ...

  7. .net分页控件webdiyer:AspNetPager

    首先下载:AspNetPager.dll   AspNetPager.xml  放到bin目录下 页面添加<%@ Register Assembly="AspNetPager" ...

  8. Python爬虫之豆瓣-新书速递-图书解析

    1- 问题描述 抓取豆瓣“新书速递”[1]页面下图书信息(包括书名,作者,简介,url),将结果重定向到txt文本文件下. 2- 思路分析[2] Step1 读取HTML Step2 Xpath遍历元 ...

  9. CentOS Linux下一个tomcat起停,查看日志的shell script

    CentOS 的tomcat安装目录:/usr/local/tomcat vi MyTomcatUitl.sh          创建文件chmod u+x MyTomcatUtil.sh   赋执行 ...

  10. DPDK内存管理-----(三)rte_malloc内存管理

    rte_malloc()为程序运行过程中分配内存,模拟从堆中动态分配内存空间. void * rte_malloc(const char *type, size_t size, unsigned al ...