【杭电oj】1242-Rescue(bfs,优先队列)

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22836    Accepted Submission(s): 8076


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
 

Author
CHEN, Xue
 

Source
ZOJ Monthly, October 2003


此题同 Battle City

有了前面的铺垫,这道题并没有什么难度。只有一个问题,这道题结构体变量n改成next的时候,会出现PE的现象,坑爹的改了半天。


代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

char map[211][211];
int vis[211][211];
int H,W;
int st_x,st_y,end_x,end_y;
int move_x[4]={0,0,1,-1};
int move_y[4]={1,-1,0,0};

struct node
{
	int x,y,stp;
	bool friend operator<(node num1,node num2)
	{
		return num1.stp>num2.stp;
	}
}a,n;

int check(int x,int y)
{
	if (x<0 || y<0 || x>=H || y>=W || vis[x][y] || map[x][y]=='#')
		return 1;
	return 0;
}

int bfs()
{
	priority_queue<node> move;
	a.x=st_x;
	a.y=st_y;
	a.stp=0;
	move.push(a);
	vis[st_x][st_y]=1;
	while (!move.empty())
	{
		a=move.top();
		move.pop();
		if (a.x==end_x && a.y==end_y)
			return a.stp;
		for (int i=0;i<4;i++)
		{
			n.x=a.x+move_x[i];
			n.y=a.y+move_y[i];
			n.stp=a.stp;
			if (check(n.x,n.y))
				continue;
			if (map[n.x][n.y]=='x')
				n.stp+=2;
			else
				n.stp++;
			vis[n.x][n.y]=1;
			move.push(n);
		}
	}
	return -1;
}

int main()
{
	while (~scanf ("%d %d",&H,&W))
	{
		for (int i=0;i<H;i++)
		{
			scanf ("%s",map[i]);
			for (int j=0;j<W;j++)
			{
				if (map[i][j]=='r')
				{
					st_x=i;
					st_y=j;
				}
				else if (map[i][j]=='a')
				{
					end_x=i;
					end_y=j;
				}
			}
		}
		memset(vis,0,sizeof(vis));
		int ans;
		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;
}



你可能感兴趣的:(【杭电oj】1242-Rescue(bfs,优先队列))