hdu 1026 Ignatius and the Princess I
题目连接
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
随机推荐
- centos 7 安装音乐播放器(亲测可用)
方法来源网上,非原创. 1. Install the nux repo $> su - $> yum update # optional but recommanded $> rp ...
- noip2009提高组题解
NOIP2009题解 T1:潜伏者 题目大意:给出一段密文和破译后的明文,一个字母对应一个密文字母,要求破译一段密文,如果有矛盾或有未出现密文无法破译输出failed,否则输出明文. 思路:纯模拟题 ...
- CSS3之选择器
总结了下CSS3新增的一些选择器. CSS3的选择器有基本选择器.属性选择器.伪类选择器几类. CSS3选择器 选择器 举例 例子描述 element1~element2 p~a 选择前面有 < ...
- Android控件大全(二)——Toolbar
1.隐藏Actionbar 代码中设置:requestWindowFeature(Window.FEATURE_NO_TITLE) //如果Activity是继承自AppCompatActiv ...
- 《Unix/Linux日志分析与流量监控》书稿完成
<Unix/Linux日志分析与流量监控>书稿完成 近日,历时3年创作的75万字书稿已完成,本书紧紧围绕网络安全的主题,对各种Unix/Linux系统及网络服务日志进行了全面系统的讲解,从 ...
- pycurl
http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2344005.html http://ju.outofmemory.cn/entry/ ...
- oracle11g空表不能导出记录
select 'alter table '||table_name||' allocate extent(size 64k);' from tabs t where not exists (selec ...
- sql case when 多条件
when 'ChangeProductName'= case --联名借姓名 --when a.ChangeProductName is not null then (substr ...
- SQL笔记 [长期更新] (-2013.7)
--IF EXISTS(SELECT * FROM dbo.SysObjects WHERE ID = object_id(N'[TABLEA]') ) DROP TABLE tableA--CREA ...
- storm启动分析
一个topology的启动包括了三个步骤 1)创建TopologyBuilder,设置输入源,输出源 2)获取config 3)提交topology(这里不考虑LocalCluster本地模式) 以s ...