题目连接

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. 单链表(c++)

    #include "stdafx.h"#include <iostream>using namespace std;const int MaxSize = 100; c ...

  2. 学习总结 java连接数据库

    package com.hanqi.test; import java.sql.*; public class jdbcTest { public static void main(String[] ...

  3. 洛谷P1984 SDOI2008烧水问题

    P1984 [SDOI2008]烧水问题 186通过 438提交 题目提供者lych 标签数论(数学相关)模拟各省省选 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 求助! 也是醉了... ...

  4. 二模11day1解题报告

    T1.树的重量(weight) 给出一棵n个叶节点的树(但是有多组数据)以及n个节点之间的距离(最短距离...然而也只有一条路),求树的所有边权之和. 一开始完全没有思路啊...难道爆搜模拟??狂汗. ...

  5. I/O系统,多线程、图形用户界面编程

    多线程 进程与线程区别: 进程需要分配独立的内存空间:线程在同一内存空间中工作,可以共享同一块内存和系统资源 与Java相关的API: 1)Thread类 方法:start()启动: urn() : ...

  6. ifdown eth0 && idup eth0 ifdown --exclude=l0 -a && ifup --exclude=lo -a

  7. js控制div颜色

    <html><head></head><style>#div1{width:400px;height:400px;background-color:re ...

  8. JSON-JQuery常用技巧

    1:Jquery对象选择查找 var group = $(".classeslist li"); class 为 classeslist 内部 的所有 li 元素对象 遍历: fo ...

  9. 【UEditor】远程上传图片到【七牛云存储】

    杂谈:最近在玩一个第三方的微信开发平台,里面的图片都是上传到[七牛云存储]的,用了一下非常的好用,支持各种语言,SDK齐全.支持全分布式系统架构以及存储技术和数据加速,于是决定将网站的图片都存储到七牛 ...

  10. POJ C程序设计进阶 编程题#3: 发票统计

    来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 有一个小型的报账系统,它有如 ...