HDU 2612

题目连接:Find a way

 

 题目大意:Y 和 M 想在 KFC 见面,找到一条最短的路,其中 # 是障碍,不能走这里。

思路:保存每一个 KFC 的位置,计算从每一个 KFC 到这两个人的距离即可。。。

But ... 为啥会 MLE !

 

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct Node{
	int x;
	int y;
	int step;
};
int n,m,ans,minn;
char a[201][201];
int vis[201][201];
int xx[4] = {0,0,1,-1};
int yy[4] = {1,-1,0,0};
queue<Node>q,que;
int pd(int x,int y)
{
	if(x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#')
	return 1;
	return 0;
}
int main()
{
	while(scanf("%d%d",&n,&m) != EOF)
	{
		int tot = 0;
		while(!q.empty())
			q.pop();
		Node st,en;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>a[i][j];
				if(a[i][j] == '@')
				{
					Node now;
					now.x = i;
					now.y = j;
					q.push(now);
					a[i][j] = '#';
				}
				if(a[i][j] == 'Y')
				{
					st.x = i;
					st.y = j;
					a[i][j] == '.';
				}
				if(a[i][j] == 'M')
				{
					en.x = i;
					en.y = j;
					a[i][j] == '.';
				}
			}
		}
		minn = 99999999;
		while(!q.empty())
		{
			while(!que.empty())
				que.pop();
			memset(vis,0,sizeof(vis));
			Node no = q.front();
			q.pop();
			no.step = 0;
			que.push(no);
			int sum = 0;
			bool flag1 = 0,flag2 = 0;
			ans = 0;
			while(!que.empty())
			{
				Node now = que.front();
				que.pop();
				vis[now.x][now.y] = 1;
				if(now.x == st.x && now.y == st.y && !flag1)
				{
					flag1 = 1;
					ans += now.step;
					//cout<<"First: "<<now.step<<endl;
				}
				if(now.x == en.x && now.y == en.y && !flag2)
				{
					flag2 = 1;
					ans += now.step;
					//cout<<"Secend: "<<now.step<<endl;
				}
				if(flag1 && flag2){
					//cout<<"minn = "<<minn<<endl;
					//cout<<"ans = "<<ans<<endl<<endl; 
					if(minn > ans)
					minn = ans;
					break;
				}
				for(int i=0;i<4;i++)
				{
					int tx = now.x + xx[i];
					int ty = now.y + yy[i];
					if(pd(tx,ty) && !vis[tx][ty])
					{
						Node nex;
						nex.x = tx;
						nex.y = ty;
						nex.step = now.step + 1;
						que.push(nex);
					}
				}
			}
		}
		cout<<minn*11<<endl;
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值