题目连接

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. python中时间格式

    问题:通过MySQLdb查询datetime字段,然后通过浏览器显示出来,得到的格式是:         'Thu, 19 Feb 2009 16:00:07 GMT'   (http呈现出来的格式) ...

  2. angular factory Services provider 自定义服务 工厂

    转载于 作者:海底苍鹰地址:http://blog.51yip.com/jsjquery/1602.html 1.在app.js 中声明了模块的依赖 var phonecatApp = angular ...

  3. windows在远程桌面连接中使用命令行参数

    在此版本的 Windows 中,可以从搜索框("运行"对话框或命令行)启动远程桌面连接,而不是从「开始」菜单启动它. 从"运行"对话框启动远程桌面的步骤 依次单 ...

  4. 代码轮子之很简单但是挺管用的基于C# Task的模拟并发的代码

    代码轮子之很简单但是挺管用的基于C# Task的模拟并发的代码

  5. 【Linux】基于Bind_DLZ和MySQL数据的DNS搭建

    参考:http://www.linuxfromscratch.org/blfs/view/cvs/server/bind.html 参考:http://bind-dlz.sourceforge.net ...

  6. C++三大库boost、loki、stlport

    转: STL是一个标准,各商家根据这个标准开发了各自的STL版本.而在这形形色色的STL版本中,SGI STL无疑是最引人瞩目的一个.这当然是因为这个STL产品系出名门,其设计和编写者名单中,Alex ...

  7. (笔记)angular 包含关系的controller参数传递

  8. 内核linux-3.4.2支持dm9000

    当前烧写:      fs:    nfs 30000000 192.168.1.17:/work/nfs_root/first_fs_mdev.yaffs2    //这里不能使用nfs挂载,只能直 ...

  9. POJ C程序设计进阶 编程题#1:分配病房

    编程题#1:分配病房 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 某个科 ...

  10. Windows Server 2003搭建FTP服务器 实现盘符之间切换

     Serv-U中设置虚拟目录的方法 如果在E盘下有一个名为LoveHina的目录,在F盘下也有一个名为LoveHina的目录.那么,如何让使用同一个账号的用户可以同时访问这两个目录呢? 我们可以使用S ...