hdu Find a way(广搜)

/*

题意:题意:Y和M要在KFC约会,地图上有多个KFC,求在哪个KFC两人所走总距离最短,(注意KFC可以当做路)

*/

#include<iostream>
#include<queue>
#define N 210
#define inf 0xffffff
using namespace std;
int flag, n, m, dis[N][N][2], dir[4][2]={1,0, -1,0, 0,1, 0,-1}, Min;
bool mark[N][N];
char str[N][N];

struct node
{
	int x, y, step;
};

void bfs(int x, int y)
{
	node cur, next;
	int i;
	cur.x=x;cur.y=y;cur.step=0;
	queue<node>q;
	q.push(cur);
	mark[x][y]=true;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		next.step=cur.step+1;
		for( i=0; i < 4; i++ )
		{
			next.x=x=cur.x+dir[i][0];
			next.y=y=cur.y+dir[i][1];
			if(x>=0 && y>=0 && x<n && y<m && mark[x][y]==false && str[x][y]!= '#')
			{
				mark[x][y]=true;
				if(str[x][y] == '@')
					dis[x][y][flag]=next.step;
				q.push(next);
			}
		}
	}
	
}

int main()
{
	while(scanf("%d%d", &n, &m) != -1)
	{
		int i, j;
		for( i=0; i < n; i++ )
			for( j=0; j < m; j++ )
				dis[i][j][0]=dis[i][j][1]=inf;
		for( i=0; i < n; i++ )
			scanf("%s", str[i]);
		for( i=0; i < n; i++ )
		{
			for(j=0; j < m; j++ )
			{
				if(str[i][j] == 'Y')
				{
					flag=0;
					memset(mark, false, sizeof(mark));
					mark[i][j] = true;
					bfs(i, j);
				}
				else if(str[i][j] == 'M')
				{
					flag=1;
					memset(mark, false, sizeof(mark));
					mark[i][j] = true;
					bfs(i, j);
				}
			}
		}
		Min = inf;
		for( i=0; i < n; i++ )
		{
			for( j=0; j < m; j++ )
			{
				if(str[i][j]=='@' && Min > dis[i][j][0]+dis[i][j][1])
					Min = dis[i][j][0]+dis[i][j][1];
			}
		}
		printf("%d\n", Min*11);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值