HDU:2612 Find a way(双BFS+打表)

6 篇文章 0 订阅

Find a way

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


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
 

Author
yifenfei
 

Source
 

Recommend
yifenfei
题目大意:y和m要去肯德基聚餐,图中有多个kfc,他们要选的那个kfc必须到彼此的所用时间之和最小,问  最少需要多少时间(这里一格代表了11分钟)。
解题思路:将y到每点的时间和m到每点的时间用bfs打表,打好表后扫地图,如果扫到@的话算下时间之和,更新min。
这题有点坑,y不能经过m的出发点,m也不能经过y的出发点,详情看http://blog.csdn.net/wjw0130/article/details/36254119)
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
char map[210][210];
int visit[210][210];
int cnt1[210][210];//储存y到每点的步数 
int cnt2[210][210];//储存m到每点的步数 
int n,m;
int cnt,yn,ym,mn,mm;
struct node
{
	int n,m;
	int step;
};
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		cnt=0;
		int ans=INF;
		memset(cnt1,INF,sizeof(cnt1));
		memset(cnt2,INF,sizeof(cnt2));
		for(int i=0;i<n;i++)
		{
			scanf("%s",&map[i]);
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='Y')
				{
					yn=i;
					ym=j;
				}
				if(map[i][j]=='M')
				{
					mn=i;
					mm=j;
				}
			}
		}
			memset(visit,0,sizeof(visit));
			struct node start;
			start.n=yn;
			start.m=ym;
			start.step=0;
			visit[yn][ym]=1;
			map[mn][mm]='#';//这里,注意下,y走路的时候不能经过m的路,所以临时把m的坐标变成# 
			queue<node>qy;
			qy.push(start);
			while(!qy.empty())//开始给y打表 
			{
				struct node tmp=qy.front();
				qy.pop();
				cnt1[tmp.n][tmp.m]=tmp.step;
				tmp.step++;
				struct node tmp2=tmp;
				if(tmp2.n-1>=0&&visit[tmp2.n-1][tmp2.m]==0&&map[tmp2.n-1][tmp2.m]!='#')
				{
					visit[tmp2.n-1][tmp2.m]=1;
					cnt1[tmp2.n-1][tmp2.m]=tmp2.step;
					tmp2.n--;
					qy.push(tmp2);
				}
				tmp2=tmp;
				if(tmp2.n+1<n&&visit[tmp2.n+1][tmp2.m]==0&&map[tmp2.n+1][tmp2.m]!='#')
				{
					visit[tmp2.n+1][tmp2.m]=1;
					cnt1[tmp2.n+1][tmp2.m]=tmp2.step;
					tmp2.n++;
					qy.push(tmp2);
				}
				tmp2=tmp;
				if(tmp2.m-1>=0&&visit[tmp2.n][tmp2.m-1]==0&&map[tmp2.n][tmp2.m-1]!='#')
				{
					visit[tmp2.n][tmp2.m-1]=1;
					cnt1[tmp2.n][tmp2.m-1]=tmp2.step;
					tmp2.m--;
					qy.push(tmp2);
				}
				tmp2=tmp;
				if(tmp2.m+1<m&&visit[tmp2.n][tmp2.m+1]==0&&map[tmp2.n][tmp2.m+1]!='#')
				{
					visit[tmp2.n][tmp2.m+1]=1;
					cnt1[tmp2.n][tmp2.m+1]=tmp2.step;
					tmp2.m++;
					qy.push(tmp2);
				}
			}
			map[mn][mm]='M';//还原m 
			memset(visit,0,sizeof(visit));//这里记得在清0以下 
			start.n=mn;
			start.m=mm;
			start.step=0;
			visit[mn][mm]=1;
			map[yn][ym]='#';//m不能走y所在的地址 
			queue<node>qm;
			qm.push(start);
			while(!qm.empty())//开始给m打表 
			{
				struct node tmp=qm.front();
				qm.pop();
				tmp.step++;
				struct node tmp2=tmp;
				if(tmp2.n-1>=0&&visit[tmp2.n-1][tmp2.m]==0&&map[tmp2.n-1][tmp2.m]!='#')
				{
					visit[tmp2.n-1][tmp2.m]=1;
					cnt2[tmp2.n-1][tmp2.m]=tmp2.step;
					tmp2.n--;
					qm.push(tmp2);
				}
				tmp2=tmp;
				if(tmp2.n+1<n&&visit[tmp2.n+1][tmp2.m]==0&&map[tmp2.n+1][tmp2.m]!='#')
				{
					visit[tmp2.n+1][tmp2.m]=1;
					cnt2[tmp2.n+1][tmp2.m]=tmp2.step;
					tmp2.n++;
					qm.push(tmp2);
				}
				tmp2=tmp;
				if(tmp2.m-1>=0&&visit[tmp2.n][tmp2.m-1]==0&&map[tmp2.n][tmp2.m-1]!='#')
				{
					visit[tmp2.n][tmp2.m-1]=1;
					cnt2[tmp2.n][tmp2.m-1]=tmp2.step;
					tmp2.m--;
					qm.push(tmp2);
				}
				tmp2=tmp;
				if(tmp2.m+1<m&&visit[tmp2.n][tmp2.m+1]==0&&map[tmp2.n][tmp2.m+1]!='#')
				{
					visit[tmp2.n][tmp2.m+1]=1;
					cnt2[tmp2.n][tmp2.m+1]=tmp2.step;
					tmp2.m++;
					qm.push(tmp2);
				}
			}
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if(map[i][j]=='@')
					{
						ans=min(ans,cnt1[i][j]+cnt2[i][j]);//更新ans 
					}
				}
			}
			printf("%d\n",ans*11);//一步当11步,题上说的 
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值