[kuangbin带你飞]专题1 简单搜索 N - Find a way HDU - 2612

题目:

Find a way

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


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


题意:Y和M两个人从当前位置出发,找一个@相遇,使Y和M到达@的时间之和最小,并求最小的到达时间。每一步耗时11min。

思路:BFS两次,第一次求Y到达所有可以到达的地方的最小时间。第二次求M到达所有可以到达地方的最小时间。最后遍历整个地图找一个两者都能到达的@,使Y和M到达此地的时间之和最小(*11).


#include<bits/stdc++.h>

typedef long long ll;

using namespace std;

const int MAXN = 210;

int n,m;
int Yx,Yy;
int Mx,My;
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
char mapp[MAXN][MAXN];
int ytime[MAXN][MAXN];   //记录y到达(i,j)的最小时间 
int mtime[MAXN][MAXN];   //记录m到达(i,j)的最小时间 

void bfs1(){
	queue< pair<int,int> > q;
	memset(ytime,-1,sizeof(ytime));
	
	q.push(make_pair(Yx,Yy));
	ytime[Yx][Yy] = 0;
	
	while (!q.empty()){
		pair<int,int> temp = q.front();
		q.pop();
		int x = temp.first;
		int y = temp.second; 
		
		for(int i = 0 ; i<4 ; i++){
		 	int nxtx = x + dir[i][0];
		 	int nxty = y + dir[i][1];
		 	
		 	if(nxtx<0 || nxty<0 || nxtx>=n || nxty>=m)
		 		continue;
		 	if(mapp[nxtx][nxty] == '#')
		 		continue;
		 	if(ytime[nxtx][nxty] != -1)
		 		continue;
		 	
		 	ytime[nxtx][nxty] = ytime[x][y] + 1;
		 	q.push(make_pair(nxtx,nxty));
		}
	}
}

void bfs2(){
	queue< pair<int,int> > q;
	memset(mtime,-1,sizeof(mtime));
	
	q.push(make_pair(Mx,My));
	mtime[Mx][My] = 0;
	
	while (!q.empty()){
		pair<int,int> temp = q.front();
		q.pop();
		int x = temp.first;
		int y = temp.second; 
		
		for(int i = 0 ; i<4 ; i++){
		 	int nxtx = x + dir[i][0];
		 	int nxty = y + dir[i][1];
		 	
		 	if(nxtx<0 || nxty<0 || nxtx>=n || nxty>=m)
		 		continue;
		 	if(mapp[nxtx][nxty] == '#')
		 		continue;
		 	if(mtime[nxtx][nxty] != -1)
		 		continue;
		 	
		 	mtime[nxtx][nxty] = mtime[x][y] + 1;
		 	q.push(make_pair(nxtx,nxty));
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	while (cin>>n>>m){
		for(int i = 0 ; i<n ; i++){
			cin>>mapp[i];
		}
		for(int i = 0 ; i<n ; i++){
			for(int j = 0 ; j<m ; j++){
				if(mapp[i][j] == 'Y'){
					Yx = i,Yy = j;
				}
				else if(mapp[i][j] == 'M'){
					Mx = i,My = j;
				}
			}
		}
		bfs1();
		bfs2();

		int ans = 1<<30;      
		for(int i = 0 ; i<n ; i++){
			for(int j = 0 ; j<m ; j++){
				if(mapp[i][j] == '@'){
					if(ytime[i][j]>=0 && mtime[i][j]>=0){     //y和m均可以到达 
						ans = min(ans,(ytime[i][j]+mtime[i][j])*11);  //更新最小值 
					}
				} 
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值