HDU 1242 Rescue(BFS)

Problem Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel’s friends want to save Angel. Their task is: approach Angel. We assume that “approach Angel” is to get to the position where Angel stays. When there’s a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. “.” stands for road, “a” stands for Angel, and “r” stands for each of Angel’s friend.

Process to the end of the file.

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing “Poor ANGEL has to stay in the prison all his life.”

Sample Input

7 8
#.#####.
#.a#…r.
#…#x…
…#…#.#
#…##…
.#…

Sample Output

13

题目大意:从r出发,找一条到a的最短路径。其中‘#’是墙壁不能走,'x’是士兵,走到士兵头上需要花费1个单位时间,然后每次上下左右移动都会花费一个单位时间。如果无法救援成功,输出Poor ANGEL has to stay in the prison all his life.

题目就是常规的广搜,如果遇到’x’士兵,那么下一个结点step + 2即可
本题大部分时间花到查错上面去了,不知道为什么tag数组用memset初始化就有错误答案,后来自己写了一个Clear函数才AC了

/*
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

3 3
r#a
.#.
...
*/
#include<iostream>
#include<queue>
#include<memory.h>
using namespace std;

const int maxn = 220;
int n, m;
char maze[maxn][maxn];
int tag[maxn][maxn];

struct Node{
	int x, y;
	int step;
	Node(int x, int y, int step)
	{
		this->x = x;
		this->y = y;
		this->step = step;
	}
};

bool panduan(int x, int y)
{
	if(tag[x][y] == 1)
		return false;
	if(maze[x][y] == '#')
		return false;
	if(x < 1 || y < 1 || x > n || y > m)	//这里笔误导致第一次提交WA 
		return false;
	return true;
}

int bfs(int x, int y)
{
	queue<Node> q;
	q.push(Node(x, y, 0));
	tag[x][y] = 1;
	while(!q.empty())		//遇到士兵step + 2就行了, 其他的和之前bfs没区别 
	{
		Node f = q.front();
		if(maze[f.x][f.y] == 'a')
			return f.step;
		if(panduan(f.x, f.y + 1))
		{
			tag[f.x][f.y + 1] = 1;
			if(maze[f.x][f.y + 1] == 'x')
				q.push(Node(f.x, f.y + 1, f.step + 2));
			else
				q.push(Node(f.x, f.y + 1, f.step + 1));
		}
		if(panduan(f.x + 1, f.y))
		{
			tag[f.x + 1][f.y] = 1;
			if(maze[f.x + 1][f.y] == 'x')
				q.push(Node(f.x + 1, f.y, f.step + 2));
			else
				q.push(Node(f.x + 1, f.y, f.step + 1));
		}
		if(panduan(f.x, f.y - 1))
		{
			tag[f.x][f.y - 1] = 1;
			if(maze[f.x][f.y - 1] == 'x')
				q.push(Node(f.x, f.y - 1, f.step + 2));
			else
				q.push(Node(f.x, f.y - 1, f.step + 1));
		}
		if(panduan(f.x - 1, f.y))
		{
			tag[f.x - 1][f.y] = 1;
			if(maze[f.x - 1][f.y] == 'x')
				q.push(Node(f.x - 1, f.y, f.step + 2));
			else
				q.push(Node(f.x - 1, f.y, f.step + 1));
		}
		q.pop();
	}
	return -1;
}

void Clear()
{
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= m;j++)
		{
			tag[i][j] = 0;
		}
	}
}

int main()
{
	while(~scanf("%d%d", &n, &m))
	{
		Clear();			//这个地方用memset函数不知为何就WA 
		for(int i = 1;i <= n;i++)
		{
			for(int j = 1;j <= m;j++)
			{
				cin >> maze[i][j];
			}
		}
		int xs, ys;
		for(int i = 1;i <= n;i++)
		{
			for(int j = 1;j <= m;j++)
			{
				if(maze[i][j] == 'r')
				{
					xs = i;
					ys = j;
				}
			}
		}
		int res = bfs(xs, ys);
		//题目要求的,看掉了,第二次提交也WA了 
		if(res == -1)
		{
			cout << "Poor ANGEL has to stay in the prison all his life."  << endl;
		}
		else
		{
			cout << res << endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值