hdu4308之BFS

Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2099    Accepted Submission(s): 727


Problem Description
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.
 

Input
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.
 

Output
One line with an integer, representing the minimum cost. If Prince ykwd cannot rescue his princess whatever he does, then output "Damn teoy!".(See the sample for details.)
 

Sample Input
  
  
1 3 3 Y*C 1 3 2 Y#C 1 5 2 YP#PC
 

Sample Output
  
  
3 Damn teoy! 0
题意:给定n行m列地图,每走一步'*'格子花费v的代价,遇到'p'可以直接跳到所有的p位置去,问从y到c的最小花费

分析:裸的BFS,遇到p就把所有含有p的位置放到队列中并标记,然后继续搜就行了,注意把所有的p位置放到队列中要把以前队列含有的点出队列在放进队列或者用优先队列,因为原来在队列中的点的花费的价值可能更大

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=5000+10;
string Map[MAX];
int n,m,v,size,s[MAX][2];//s记录p的位置
int dir[4][2]={0,1,0,-1,1,0,-1,0};
bool mark[MAX];

struct Node{
	int x,y,t;
	Node(){}
	Node(int X,int Y,int T):x(X),y(Y),t(T){}
}start;

void CalculateP(){
	size=0;
	for(int i=0;i<n;++i){
		for(int j=0;j<m;++j){
			if(Map[i][j] == 'P'){
				s[size][0]=i;
				s[size][1]=j;
				++size;
			}else if(Map[i][j] == 'Y'){
				start=Node(i,j,0);
			}
		}
	}
}

int BFS(){
	memset(mark,false,sizeof mark);
	 queue<Node>q;
	 Node oq,next;
	 q.push(start);
	 mark[start.x*m+start.y]=true;
	 while(!q.empty()){
 		oq=q.front();
 		q.pop();
 		for(int i=0;i<4;++i){
		 	next=Node(oq.x+dir[i][0],oq.y+dir[i][1],oq.t);;
		 	if(next.x<0 || next.y<0 || next.x>=n || next.y>=m)continue;
		 	if(mark[next.x*m+next.y] || Map[next.x][next.y] == '#')continue;
		 	mark[next.x*m+next.y]=true;
		 	if(Map[next.x][next.y] == 'C')return next.t;
		 	if(Map[next.x][next.y] == 'P'){
		 		int len=q.size();
	 			for(int j=0;j<size;++j){
			 		mark[s[j][0]*m+s[j][1]]=true;
			 		q.push(Node(s[j][0],s[j][1],oq.t));
			 	}
			 	while(len--)next=q.front(),q.pop(),q.push(next);
	 		}else{
		 		next.t+=v;
		 		q.push(next);
		 	}
		 }
 	}
 	return -1;
}

int main(){
	while(cin>>n>>m>>v){
		for(int i=0;i<n;++i)cin>>Map[i];
		CalculateP();
		int temp=BFS();
		if(temp<0)cout<<"Damn teoy!"<<endl;
		else cout<<temp<<endl;
	}
	return 0;
}


“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值