博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1026 Ignatius and the Princess I
阅读量:5338 次
发布时间:2019-06-15

本文共 5872 字,大约阅读时间需要 19 分钟。

题目连接

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+路径记录。。

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using std::cin;10 using std::cout;11 using std::endl;12 using std::find;13 using std::sort;14 using std::pair;15 using std::vector;16 using std::multimap;17 using std::priority_queue;18 #define pb(e) push_back(e)19 #define sz(c) (int)(c).size()20 #define mp(a, b) make_pair(a, b)21 #define all(c) (c).begin(), (c).end()22 #define iter(c) decltype((c).begin())23 #define cls(arr,val) memset(arr,val,sizeof(arr))24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)27 const int Max_N = 110;28 const int Mod = 110;29 typedef unsigned long long ull;30 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };31 bool vis[Max_N][Max_N];32 char maze[Max_N][Max_N];33 int N, M, res, fa[Max_N][Max_N], dir[Max_N][Max_N];34 struct Node {35 int x, y, s;36 Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}37 inline bool operator<(const Node &a) const {38 return s > a.s;39 }40 };41 void bfs() {42 cls(vis, false), cls(fa, 0), cls(dir, 0);43 priority_queue
q;44 q.push(Node());45 while (!q.empty()) {46 Node t = q.top(); q.pop();47 if (t.x == N - 1 && t.y == M - 1) { res = t.s; return; }48 rep(i, 4) {49 int nx = t.x + dx[i], ny = t.y + dy[i];50 char &ch = maze[nx][ny];51 if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue;52 if (ch == 'X' || vis[nx][ny]) continue;53 if ('0' <= ch && ch <= '9') q.push(Node(nx, ny, t.s + 1 + ch - '0'));54 else q.push(Node(nx, ny, t.s + 1));55 vis[nx][ny] = true;56 fa[nx][ny] = t.x * Mod + t.y;57 dir[nx][ny] = i;58 }59 }60 res = -1;61 }62 void show_path(int x, int y) {63 if (-1 == res) { printf("God please help our poor hero.\nFINISH\n"); return; }64 int fx, fy;65 vector
path;66 for (;;) {67 fx = fa[x][y] / Mod;68 fy = fa[x][y] % Mod;69 if (!x && !y) break;70 path.push_back(dir[x][y]);71 x = fx, y = fy;72 }73 printf("It takes %d seconds to reach the target position, let me show you the way.\n", res);74 fx = fy = 0;75 for (int i = sz(path) - 1, j = 1; ~i && j <= res; i--) {76 x = fx + dx[path[i]], y = fy + dy[path[i]];77 char &ch = maze[x][y];78 if ('0' <= ch && ch <= '9') {79 int t = ch - '0';80 printf("%ds:(%d, %d)->(%d, %d)\n", j++, fx, fy, x, y);81 while (t--) printf("%ds:FIGHT AT (%d,%d)\n", j++, x, y);82 }83 else printf("%ds:(%d, %d)->(%d, %d)\n", j++, fx, fy, x, y);84 fx = x, fy = y;85 }86 puts("FINISH");87 }88 int main() {89 #ifdef LOCAL90 freopen("in.txt", "r", stdin);91 freopen("out.txt", "w+", stdout);92 #endif93 while (~scanf("%d %d", &N, &M)) {94 rep(i, N) scanf("%s", maze[i]);95 bfs();96 show_path(N - 1, M - 1);97 }98 return 0;99 }
View Code

转载于:https://www.cnblogs.com/GadyPu/p/4612847.html

你可能感兴趣的文章
手把手教你做手机婚恋网
查看>>
.net控制台程序Program args参数解析
查看>>
hdu2824(欧拉函数)
查看>>
C#-在主线程中创建子线程
查看>>
2012-7-29Joomla自带的文章模块+K2文章模块+ZT文章模块
查看>>
bzoj1018[SHOI2008]堵塞的交通traffic
查看>>
day22_1-课前上节复习+os模块
查看>>
python (五)小数据池、集合、copy、文件操作
查看>>
Nginx rewrite URL examples with and without redirect address
查看>>
浮点类型的特殊性知多少
查看>>
继承(一)
查看>>
【NOIP模拟】花花森林
查看>>
copy和mutableCopy
查看>>
这是我的第一篇播客,多多指教
查看>>
C语言编程经典100例
查看>>
Python3——根据m3u8下载视频(上)之urllib.request
查看>>
使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(四)-- Middleware
查看>>
Java实现快速排序
查看>>
小程序的生命周期
查看>>
linux之 文本编辑 的基础知识点
查看>>