杭电ACM1242——Rescue~~BFS+优先队列

这题,简单的BFS就可以搞定。题目的大概意思是最短时间从地图的r到达a。

一开始,用普通的队列来做,结果内存超了,原因是N和M最大200;普通的队列会浪费一大堆内存,所以应该改用优先队列来做。

下面是AC的代码:

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
class data
{
public:
	int x, y, cost;
	friend bool operator < (data a, data b)    //按最小堆来建堆
	{
		return b.cost < a.cost;
	}
};
int N, M;
char map[205][205];
int sx, sy, ex, ey;
int xy[4][2] = {1,0,-1,0,0,1,0,-1};

int bfs()                                   //BFS搜索地图,走过的标记为#
{
	priority_queue<data>que;
	data temp;
	temp.x = sx; temp.y = sy; temp.cost = 0;
	que.push(temp);
	map[sx][sy] = '#';
	while(!que.empty())
	{
		temp = que.top();
		que.pop();
		if(temp.x == ex && temp.y == ey)
			return temp.cost;
		int fx, fy;
		for(int i = 0; i < 4; i++)               //四个方向搜索
		{
			fx = temp.x + xy[i][0];
			fy = temp.y + xy[i][1];
			if(fx >= 0 && fx < N && fy >= 0 && fy < M && map[fx][fy] != '#')   //判断是否符号条件
			{
				if(map[fx][fy] == '.' || map[fx][fy] == 'a')               //判断是否是卫兵,
				{
					data te;
					te.x = fx; te.y = fy; te.cost = temp.cost + 1;
					que.push(te);
					map[fx][fy] = '#';
				}
				else                                                        //是卫兵,需要两个单位时间
				{
					data te;
					te.x = fx; te.y = fy; te.cost = temp.cost + 2;
					que.push(te);
					map[fx][fy] = '#';
				}
			}
		}
	}
	return -1;
}

int main()
{
	freopen("data.txt", "r", stdin);
	while(scanf("%d%d", &N, &M) != EOF)
	{
		getchar();
		for(int i = 0; i < N; i++)
		{
			for(int j = 0; j < M; j++)
			{
				scanf("%c", &map[i][j]);
				if(map[i][j] == 'r')
				{
					sx = i;
					sy = j;
				}
				if(map[i][j] == 'a')
				{
					ex = i;
					ey = j;
				}
			}
			getchar();
		}
		int ans = bfs();
		if(ans == -1)
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		else
			printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-08-12 08:26:26

杭电ACM1242——Rescue~~BFS+优先队列的相关文章

杭电1242--Rescue(BFS+优先队列)

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20821    Accepted Submission(s): 7429 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is d

杭电 1242 Rescue(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

hdu 1242 Rescue(bfs+优先队列)

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

poj1649 Rescue(BFS+优先队列)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

Catch That Cow 杭电2717【BFS】

Problem Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. F

杭电483--Nightmare(Bfs)

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial e

杭电2102--A计划(Bfs)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2102 骑士救公主,迷宫问题.做的时候思路不清晰,一直Wa, 其实就是没搞清楚从posa-->posb无论b是传送门还是路都会耗时1, 只不过经过传送门又传送到了下一个位置:一直在这个误区里走不出来:Tmdi.bfs搜一遍就说明搜过来就说明posa一定为点,代码中又把特殊情况全部排除, 所以只有两种情况: '.' --->'#', '.'-->'.'(包含p, 为搜索结束条件), 瞬间明朗了很多啊

杭电 逃离迷宫 BFS

给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去.令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的.我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一