Find a way HDU - 2612

Find a way HDU - 2612

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,刚开始写时每组数据bfs要进行多次,超时。开始优化,结果一直超时,心态炸了。。最后看到杭电oj评论区上一个人说改为1500过了,试了试,,,真过了(感谢)。。(优化时没有重新读题)。再一读题,范围[2,200]。之前一直以为范围到100,设的是105(和上题弄混了)。卧槽啊。。。因为这,近俩小时没了。。。。

#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<map>
using namespace std;

const int inf=1000000;
char a[1050][1050];//刚开始没注意范围。。。 
bool vis[1050][1050];
int n,m,a1,a2,b1,b2,ans;
int dx[]={0,1,0,-1};//方位数组 
int dy[]={1,0,-1,0};
int m1[1050][1050],m2[1050][1050];//分别记录每人到每个目的地所需的步数,下标即为坐标 

struct node{//方位,步数 
	int x,y; 
	int step;
};


bool judge(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m)
	{
		return false;
	}
	if(a[x][y]=='#'||vis[x][y]||a[x][y]=='M'||a[x][y]=='Y')//M与Y也不能经过,感谢杭电oj评论区及里面的人 
	{
		return false;
	}
	return true;
}

void bfs(int x,int y,int c)
{
	node cur,next;
	queue<node>q;
	cur.x=x;
	cur.y=y;
	cur.step=0;
	q.push(cur);
	vis[cur.x][cur.y]=true;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		if(a[cur.x][cur.y]=='@')
		{
			if(c==1)
			{
				m1[cur.x][cur.y]=cur.step; 
			}
			else
			{
				m2[cur.x][cur.y]=cur.step;
			}
		}
		for(int i=0;i<4;i++)
		{
			next.x=cur.x+dx[i];
			next.y=cur.y+dy[i];
			if(!judge(next.x,next.y))
			{
				continue;
			}
			vis[next.x][next.y]=true;
			next.step=cur.step+1;
			q.push(next);
		}
	}
	return;
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(m1,0,sizeof(m1));//初始化 
		memset(m2,0,sizeof(m2));
		int i,j,sum=0;
		ans=inf;
		for(i=0;i<n;i++)
		{
			scanf("%s",a[i]);
			for(j=0;j<m;j++)
			{
				if(a[i][j]=='Y')//记录起始坐标 
				{
					a1=i;
					a2=j;
				}
				if(a[i][j]=='M')
				{
					b1=i;
					b2=j;
				}
			}
		}
		memset(vis,0,sizeof(vis));
		bfs(a1,a2,1);
		memset(vis,0,sizeof(vis));//两个bfs共用一个vis,需要初始化 
		bfs(b1,b2,2);
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(a[i][j]=='@'&&m1[i][j]!=0&&m2[i][j]!=0)
				{
					if(ans>m1[i][j]+m2[i][j])
					{
						ans=m1[i][j]+m2[i][j];
					}
				}
			}
		}
		printf("%d\n",ans*11);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值