hdu 2616

时隔1年再来写这道题,居然还是没写出来,看了自己以前的代码和discuss才明白,就是不能再次经过Y和M的点。

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

int n,m;
char a[300][300];

int cot[300][300];
bool vis[300][300];
struct point
{
	int x;
	int y;
	int step;
};
int ans;
queue<point> q;
int dir[][2]={{1,0},{0,1},{0,-1},{-1,0}};
void bfs(int x,int y)
{
	while(!q.empty())
		q.pop();
	vis[x][y]=1;
	point t;
	t.x=x;
	t.y=y;
	t.step=0;
	q.push(t);
	point hd;
	int i,j;
	int xx,yy;
	while(!q.empty())
	{
		hd=q.front();
		q.pop();
		for(i=0;i<4;i++)
		{
			xx=hd.x+dir[i][0];
			yy=hd.y+dir[i][1];
			if(xx<0||yy<0||xx>=n||yy>=m||vis[xx][yy]==1||a[xx][yy]=='#')
				continue;
			if(a[xx][yy]=='@'&&cot[xx][yy]!=0)//去掉只能有一个人能到达的kfc,因为到这个的kfc可能恰好被他们的房子挡住了
			{
				ans=min(ans,cot[xx][yy]+hd.step+1);
			}
			t.x=xx;
			t.y=yy;
			cot[xx][yy]+=hd.step+1;
			t.step=hd.step+1;
			vis[xx][yy]=1;
			q.push(t);
		}
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	while(cin>>n>>m)
	{
		 ans=0x3f3f3f3f;
		int i,j;
		int sx,sy;
		int ex,ey;
		memset(cot,0,sizeof(cot));
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				cin>>a[i][j];
				if(a[i][j]=='Y')
				{
					sx=i;
					sy=j;
					a[i][j]='#';
				}
				else if(a[i][j]=='M')
				{
					ex=i;
					ey=j;
					a[i][j]='#';
				}
			}

		bfs(sx,sy);
		memset(vis,0,sizeof(vis));
		bfs(ex,ey);
		cout<<ans*11<<endl;
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值