Find a way

这道题目需要用到两次广搜,分别找出两个人到各个kfc花费的时间(有的人可能无法到达所有的kfc,而且只要不是#就都能走)

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

#include<stdio.h>
#include<string.h >
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct list
{
	int x;
	int y;
	int step;
};
int n,m;
bool check(int x,int y);
bool book[205][205];
char fig[205][205];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
vector<struct list> vy,vm;
queue<struct list> q;
int main()
{	
	while(scanf("%d%d",&n,&m)!=EOF) 
	{
		int _min;		
		struct list Y,M;
		vy.clear();
		vm.clear();
		memset(fig,0,sizeof(fig));
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				scanf(" %c",&fig[i][j]);
				if(fig[i][j]=='Y')
				{
					Y.x=i;
					Y.y=j;
					Y.step=0;
				}
				if(fig[i][j]=='M')
				{
					M.x=i;
					M.y=j;
					M.step=0;
				}
			}
		}					
		struct list ne;
		q.push(Y);		
		memset(book,false,sizeof(book));
		book[Y.x][Y.y]=true;
		while(!q.empty())
		{
			struct list Q;
			Q=q.front();
			for(int i=0;i<4;i++)
			{
				if(check(Q.x+dir[i][0],Q.y+dir[i][1]))
				{
					book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;
					ne.x=Q.x+dir[i][0];
					ne.y=Q.y+dir[i][1];
					ne.step=Q.step+1;
					q.push(ne);
					if(fig[Q.x+dir[i][0]][Q.y+dir[i][1]]=='@')
					{
						vy.push_back(ne);
					}
				}
			}
			q.pop();
		}
		q.push(M);
		memset(book,false,sizeof(book));
		book[M.x][M.y]=true;
		while(!q.empty())
		{
			struct list Q;
			Q=q.front();
			for(int i=0;i<4;i++)
			{
				if(check(Q.x+dir[i][0],Q.y+dir[i][1]))
				{
					book[Q.x+dir[i][0]][Q.y+dir[i][1]]=true;
					ne.x=Q.x+dir[i][0];
					ne.y=Q.y+dir[i][1];
					ne.step=Q.step+1;
					q.push(ne);
					if(fig[Q.x+dir[i][0]][Q.y+dir[i][1]]=='@')
					{
						vm.push_back(ne);
					}
				}
			}
			q.pop();
		}
		_min=0x3f3f3f3f;	
		for(int i=0;i<vy.size();i++)
		{
			for(int j=0;j<vm.size();j++)
			{
				if(vy[i].x==vm[j].x&&vy[i].y==vm[j].y)
					_min=min(_min,vy[i].step+vm[j].step);					
			}
		}
		printf("%d\n",_min*11);		
	}
	return 0;
}
bool check(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m)
		if(book[x][y]==false)
			if(fig[x][y]!='#')
				return true;
	return false;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值