HDU 2612 Find a way

Find a way

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


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

Sample Output
  
  
66 88 66
分析:该题目为双向BFS题,思路见代码注释;
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,mm;
char map[205][205];
int vy[205][205],vm[205][205];
int dir[][2]={0,1,1,0,-1,0,0,-1};
struct Node{
	int x,y;
	int dis;
};
queue<Node> q;
queue<Node> q2;
void init()
{
	memset(vy,0,sizeof(vy));
	memset(vm,0,sizeof(vm));
	while(!q.empty())
		q.pop();
}
int judge(Node ss)
{
	if(ss.x>=0 && ss.x<n && ss.y>=0 && ss.y<m)
		return 1;
	return 0;
}
void bfsy(int x,int y)
{
	Node asd;
	asd.x=x;	asd.y=y;	asd.dis=0;
	vy[x][y]=1;
	q.push(asd);
	while(!q.empty())
	{
		Node tep=q.front();
		q.pop();
		if(map[tep.x][tep.y]=='@')
		{
			vy[tep.x][tep.y]=tep.dis;//访问记录上,顺便放距离 
		}
		for(int i=0;i<4;i++)
		{
			Node ss;
			ss.x=tep.x+dir[i][0];
			ss.y=tep.y+dir[i][1];
			ss.dis=tep.dis+1;
			//判定条件不越界,且未访问,且路通 
			if(judge(ss) && !vy[ss.x][ss.y] && (map[ss.x][ss.y]=='.' || map[ss.x][ss.y]=='@'))
			{
				q.push(ss);
				vy[ss.x][ss.y]=1;
			}
		}
	}
}
void bfsm(int x,int y)
{
	Node asd;
	asd.x=x;	asd.y=y;	asd.dis=0;
	vm[x][y]=1;
	q.push(asd);
	while(!q.empty())
	{
		Node tep=q.front();
		q.pop();
		if(map[tep.x][tep.y]=='@')
		{
			vm[tep.x][tep.y]=tep.dis;
		}
		for(int i=0;i<4;i++)
		{
			Node ss;
			ss.x=tep.x+dir[i][0];
			ss.y=tep.y+dir[i][1];
			ss.dis=tep.dis+1;
			if(judge(ss) && !vm[ss.x][ss.y] && (map[ss.x][ss.y]=='.' || map[ss.x][ss.y]=='@'))
			{
				q.push(ss);
				vm[ss.x][ss.y]=1;
			}
		}
	}
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init();//每次恢复初始状态 
		for(int i=0;i<n;i++)
			scanf("%s",map[i]);
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='Y')//找到Y从Y开始bfs 
				{
					bfsy(i,j);
					while(!q.empty())
						q.pop();
				}
				if(map[i][j]=='M')//找到M从M开始bfs 
				{
					bfsm(i,j);
					while(!q.empty())
						q.pop();
				}
				if(map[i][j]=='@')//找到@记录@,后面会用到 
				{
					Node ssd;
					ssd.x=i; ssd.y=j;
					q2.push(ssd);
				}
			}
		}
		mm=2147483647;
		//两个bfs都结束了,拿出所存的@,并比较出最小值。 
		while(!q2.empty())
		{
			Node ssd=q2.front();
			q2.pop();
			if(vy[ssd.x][ssd.y] && vm[ssd.x][ssd.y])
			{
				if(vy[ssd.x][ssd.y]+vm[ssd.x][ssd.y]<mm)
					mm=vy[ssd.x][ssd.y]+vm[ssd.x][ssd.y];
			}
		}
		printf("%d\n",11*mm);
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值