您的位置:首页 > 其它

HDU - 1026 - Ignatius and the Princess I(bfs)

2016-07-27 19:00 357 查看


Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 17058    Accepted Submission(s): 5459
Special Judge


Problem 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

 

题意:一个骑士要去救公主,恶龙的巢穴是一个迷宫,X是陷阱不可以走,n则表明这里有一个怪兽,需要花n秒解决。求从左上角走到右下角的最短路径并输出,若没有,输出God.......

本来想写dfs,不过都说是bfs,有个博客具体讲了bfs与dfs的区别,觉得自己最近做搜索真是蠢透了= =

dfs搜索有限制步数的最短路很方便,但是它对同一点要搜索很多遍,所以如果不限制步数会对剪枝的要求很高,不然会一直TLE。

bfs搜索没有限制的最短路是最好的,因为每个点只搜索一遍,就可以省去很多重复搜索节省时间。

这道题目用bfs搜索,第一次写优先队列有点小激动【捂脸】,不过总算有写优先队列的经验了。

大神很强势,从终点开始搜,省去了存图后还要丢进栈里的空间。搜索并存它的前一个点,找到结果后回溯输出路径。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <assert.h>
#define INF 0x3f3f3f3f
using namespace std;
const int M = 105;

struct node
{
int x, y, times;
friend bool operator < (node a, node b) {
return a.times > b.times;  //按时间从小到大排序
}
};

struct Pre
{
int px, py;
}pre[M][M];  //存每个点的前一个点

int n, m;
int ans;
char maps[M][M];
bool vis[M][M];  //存每个点是否搜索过,若搜索过,不用继续搜索
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

bool judge(int x, int y)
{
if (x < 0 || y < 0 || x >= n || y >= m || maps[x][y] == 'X')
return false;
return true;
}

void bfs(int x, int y)
{
vis[x][y] = true;
pre[x][y].px = -1;
node ft, nt;
ft.x = x, ft.y = y, ft.times = 0;
if (maps[x][y] != '.')
ft.times = maps[x][y] - '0';
priority_queue<node> q; //优先队列【捂脸】
q.push(ft);
while (!q.empty()) {
ft = q.top();
q.pop();

if (ft.x == 0 && ft.y == 0) { //走到起点便开始回溯输出结果
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ft.times);
x = ft.x, y = ft.y;
int anscnt = 0;
while (pre[x][y].px != -1) { //回溯回溯回溯,通过前导点的记录循环回溯(写这里想起了spfa的存图)
int tx = pre[x][y].px;
int ty = pre[x][y].py;
printf("%ds:(%d,%d)->(%d,%d)\n", ++anscnt, x, y, tx, ty);
if (maps[tx][ty] != '.')
for (int kk = 0; kk < maps[tx][ty] - '0'; kk++)
printf("%ds:FIGHT AT (%d,%d)\n", ++anscnt, tx, ty);
x = tx, y = ty;
}
return;
}

for (int i = 0; i < 4; i++) {
nt.x = ft.x + dir[i][0];
nt.y = ft.y + dir[i][1];
if (!judge(nt.x, nt.y)) continue;
if (vis[nt.x][nt.y]) continue;
//某点搜索过便不必继续搜索,优先队列优先判断时间花费最小的点,所以下一次再扫到这个点一定不会比上一次更优
vis[nt.x][nt.y] = true;

int val;
if (maps[nt.x][nt.y] == '.')
val = 1;
else
val = maps[nt.x][nt.y] - '0' + 1;
nt.times = ft.times + val;

pre[nt.x][nt.y].px = ft.x;
pre[nt.x][nt.y].py = ft.y;

q.push(nt);
}
}
printf("God please help our poor hero.\n"); //搜索不到终点
}

int main()
{
while (scanf("%d%d", &n, &m) != EOF) {
for (int i = 0; i < n; ++i) {
scanf("%s", maps[i]);
}
memset(vis, false, sizeof(vis));
bfs(n - 1, m - 1);
printf("FINISH\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: