首页 > 搜索 > BFS搜索 > HDU 4308-Saving Princess claire_-BFS-[解题报告]HOJ
2015
05-23

HDU 4308-Saving Princess claire_-BFS-[解题报告]HOJ

Saving Princess claire_

问题描述 :

Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy.
Out of anger, little Prince ykwd decides to break into the maze to rescue his lovely Princess.
The maze can be described as a matrix of r rows and c columns, with grids, such as ‘Y’, ‘C’, ‘*’, ‘#’ and ‘P’, in it. Every grid is connected with its up, down, left and right grids.
There is only one ‘Y’ which means the initial position when Prince ykwd breaks into the maze.
There is only one ‘C’ which means the position where Princess claire_ is jailed.
There may be many ‘*’s in the maze, representing the corresponding grid can be passed through with a cost of certain amount of money, as GDM teoy has set a toll station.
The grid represented by ‘#’ means that you can not pass it.
It is said that as GDM teoy likes to pee and shit everywhere, this grid is unfortunately damaged by his ugly behavior.
‘P’ means it is a transmission port and there may be some in the maze. These ports( if exist) are connected with each other and Prince ykwd can jump from one of them to another.

They say that there used to be some toll stations, but they exploded(surely they didn’t exist any more) because of GDM teoy’s savage act(pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince ykwd has his mysterious power that he can choose his way among the wormholes, even he can choose to ignore the wormholes.
Although Prince ykwd deeply loves Princess claire_, he is so mean that he doesn’t want to spend too much of his money in the maze. Then he turns up to you, the Great Worker who loves moving bricks, for help and he hopes you can calculate the minimum money he needs to take his princess back.

输入:

Multiple cases.(No more than fifty.)
The 1st line contains 3 integers, r, c and cost. ‘r’, ‘c’ and ‘cost’ is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] )
Then an r * c character matrix with ‘P’ no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.

输出:

Multiple cases.(No more than fifty.)
The 1st line contains 3 integers, r, c and cost. ‘r’, ‘c’ and ‘cost’ is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] )
Then an r * c character matrix with ‘P’ no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.

样例输入:

1 3 3
Y*C

1 3 2
Y#C

1 5 2
YP#PC

样例输出:

3
Damn teoy!
0

/*
分析:
    简单广搜。
    无力了。。。“10%”不是对10取余的意思么?实际P点有几百个,
TLE了半天,这个范围一改大就果断a掉了。。。

                                                           2012-12-12
*/

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#define N 5001
using namespace std;

int n,m,cost;
char map[N][N];
char flag[N][N];

int s_x,s_y,e_x,e_y;
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
struct A
{
	int x,y;
}hehe[555];
int tot;

struct node
{
	int x,y;
	int cost;
	friend bool operator<(node n1,node n2)
	{
		return n2.cost<n1.cost;
	}
};

int BFS()
{
	priority_queue<node>q;
	node now,next;
	int i,j;

	for(i=0;i<n;i++)
	for(j=0;j<m;j++)
		flag[i][j]=0;
	now.x=s_x;
	now.y=s_y;
	now.cost=0;
	q.push(now);
	flag[now.x][now.y]=1;
	while(!q.empty())
	{
		now=q.top();
		q.pop();
		if(now.x==e_x && now.y==e_y)	return now.cost-cost;
		for(i=0;i<4;i++)
		{
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];

			if(next.x<0 || next.x>=n || next.y<0 || next.y>=m)	continue;
			if(map[next.x][next.y]=='#')continue;
			if(flag[next.x][next.y])	continue;

			if(map[next.x][next.y]=='*')
			{
				next.cost=now.cost+cost;
				q.push(next);
				flag[next.x][next.y]=1;
			}
			else if(map[next.x][next.y]=='P')
			{
				if(flag[next.x][next.y])	continue;
				int l;
				for(l=0;l<tot;l++)
				{
					next.x=hehe[l].x;
					next.y=hehe[l].y;
					next.cost=now.cost;
					q.push(next);
					flag[next.x][next.y]=1;
				}
			}
		}
	}
	return -1;
}
int main()
{
	int i,l;
	int ans;
	while(scanf("%d%d%d",&n,&m,&cost)!=-1)
	{
		tot=0;
		for(i=0;i<n;i++)
		{
			scanf("%s",&map[i]);
			for(l=0;map[i][l];l++)
			{
				if(map[i][l]=='Y')		{s_x=i;s_y=l;}
				else if(map[i][l]=='C')	{e_x=i;e_y=l;map[i][l]='*';}
				else if(map[i][l]=='P')	{hehe[tot].x=i;hehe[tot].y=l;tot++;}
			}
		}

		ans=BFS();
		if(ans==-1)	printf("Damn teoy!\n");
		else		printf("%d\n",ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ice_crazy/article/details/8286673


,