HDOJ 1612 Find a way(双向BFS)



http://acm.hdu.edu.cn/showproblem.php?pid=2612

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4793    Accepted Submission(s): 1631


Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
 


Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
 


Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
 


Sample Input
  
  
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#

要特别注意要Y和M都能到达你当前判断的那个KFC;Y不走M所在的点且M不走Y所在的点和Y走M所在的点且M走Y所在的点   两种情况都能AC

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX 400
#define INT_MAX 1000000
char graph[MAX][MAX];
struct KFC
{//每个KFC的坐标 
	int x,y;
}num[MAX];
int idy[MAX][MAX]/*yifenfei到图中各点的最短路*/,
    idm[MAX][MAX]/*Merceki到图中各点的最短路*/,
    xy,yy,//yifenfei的坐标 
    xm,ym;//Merceki的坐标 
    
struct Rode
{
	int x,y;
} s,p,q;
int dir[4][2]={
	{-1,0},{1,0},{0,-1},{0,1}
};
int main()
{
	int r,c,i,j,k;
	while(scanf("%d %d",&r,&c)!=EOF)
	{
		for(i=0;i<r;i++)
		scanf("%s",graph[i]);
		for(i=0,k=0;i<r;i++)
		 for(j=0;j<c;j++)
		 {
		 	if(graph[i][j]=='Y')
		 	{
		 		xy=i;yy=j;
		 	}
		 	if(graph[i][j]=='M')
		 	{
		 		xm=i;ym=j;
		 	}
		 	if(graph[i][j]=='@')
		 	{
		 		num[k].x=i;
		 		num[k].y=j;
		 		k+=1;
		 	}
	     }
	     queue<Rode>Q;
	     
	     memset(idy,0,sizeof(idy));
	     s.x=xy;
	     s.y=yy;
	     idy[s.x][s.y]=1;
	     Q.push(s);
	     while(!Q.empty())
	     {
	     	q=Q.front();
	     	Q.pop();
	     	for(i=0;i<4;i++)
	     	{
	     		int X=q.x+dir[i][0];
	     		int Y=q.y+dir[i][1];
	     		if(X<0||X>=r||Y<0||Y>=c||graph[X][Y]=='#'||graph[X][Y]=='M'||idy[X][Y]!=0) continue;
	     		idy[X][Y]=idy[q.x][q.y]+1;
				p.x=X;p.y=Y;
	     		Q.push(p);
	     	}
	     }
	     
	     memset(idm,0,sizeof(idm));
	     s.x=xm;
	     s.y=ym;
	     idm[s.x][s.y]=1;
	     Q.push(s);
	     while(!Q.empty())
	     {
	     	q=Q.front();
	     	Q.pop();
	     	for(i=0;i<4;i++)
	     	{
	     		int X=q.x+dir[i][0];
	     		int Y=q.y+dir[i][1];
	     		if(X<0||X>=r||Y<0||Y>=c||graph[X][Y]=='#'||graph[X][Y]=='Y'||idm[X][Y]!=0) continue;
	     		idm[X][Y]=idm[q.x][q.y]+1;
	     		p.x=X;p.y=Y;
	     		Q.push(p);
	     	}
	     }
	     
         
	     int min=INT_MAX;
	     for(i=0;i<k;i++)
	     {
	     	//判断是否Y和M都可到达第i个KFC,因为少了这个判断wa了很多次 
	     	if(idy[num[i].x][num[i].y]==0||idm[num[i].x][num[i].y]==0) continue;
		    if(idy[num[i].x][num[i].y]+idm[num[i].x][num[i].y]<min)
	        min=idy[num[i].x][num[i].y]+idm[num[i].x][num[i].y];
	     }
	     
	     printf("%d\n",(min-2)*11);
	 }
	 return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值