HDU 2612 Find a way BFS

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11189    Accepted Submission(s): 3672


Problem Description
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
 

Author
yifenfei
 

Source
 

Recommend
yifenfei   |   We have carefully selected several similar problems for you:   1254  1728  2102  1072  1175 
 
题意大致就是两个人约好在咖啡馆见面,然后为了节省时间吧(咱不说他懒),找个两个人都最近的,每走一个格需要11分钟吧,最后输出两人的总时间。
双向bfs,我这里是弄了个二维数组保存一下每个人到每个咖啡馆的距离,然后去加,由于一开始的数组保存全部赋初值0,所以等于0的说明没有咖啡馆,直接跳过,就可以ac了。我感觉还可以剪枝,但过了,就不剪了。
ac代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>

using namespace std;

int n,m,flag,Y[2],M[2],dis[205][205][2],vis[205][205];
char map[205][205];
int dir[4][2]={1,0,0,1,-1,0,0,-1};

struct node {
	int x,y,ceng;
};
queue<node> Q;

void init()
{
	while(!Q.empty())
	{
		Q.pop();
	}
	
}

int judge(node next)
{
//	printf("%d *** %d\n",n,m);
//	printf("%d *** %d\n",next.x,next.y);
	if(next.x>=0 && next.x<n && next.y>=0 && next.y<m)
	{
		//printf("***judge***\n");
		return 1;	
	}
	
	return 0;
}

void bfs(int *s)
{
	node tmp;
	int i,j;
	
	tmp.x=s[0];
	//printf("S.x=%d***\n",s[0]);
	tmp.y=s[1];
	//printf("S.y=%d***\n",s[1]);
	tmp.ceng=0;
	vis[tmp.x][tmp.y]=1;
	Q.push(tmp);
	while(!Q.empty())
	{
		tmp=Q.front();
		Q.pop();
		if(map[tmp.x][tmp.y]=='@')
		{
			//printf("***dis***\n");
			dis[tmp.x][tmp.y][flag]=tmp.ceng;
			vis[tmp.x][tmp.y]=1;
		}
		for(i=0;i<4;i++)
		{
			node next;
			next.ceng=tmp.ceng+1;
			
			next.x=tmp.x+dir[i][0];
			next.y=tmp.y+dir[i][1];
			if(!vis[next.x][next.y]&&map[next.x][next.y]!='#'&&judge(next))
			{
				//printf("#########\n");
				Q.push(next);
				vis[next.x][next.y]=1;
			}
		}
	}
}

int main()
{
	int i,j,min;
	
	while(~scanf("%d%d",&n,&m))
	{
		
		for(i=0;i<n;i++)
		{
			getchar();
			scanf("%s",map[i]);
			for(j=0;j<m;j++)
			{
				if(map[i][j]=='Y')
				{
					Y[0]=i;
					Y[1]=j;
				}
				else if(map[i][j]=='M')
				{
					M[0]=i;
					M[1]=j;
				}
			}
		}
		memset(dis,0,sizeof(dis));
		memset(vis,0,sizeof(vis));
		flag=0;
		init();
		bfs(Y);
		memset(vis,0,sizeof(vis));
		flag=1;
		init();
		bfs(M);
		min=2147483647;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if((dis[i][j][0]+dis[i][j][1])!=0&&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;
}



内容概要:本文档详细介绍了基于Python的在线二手电子产品回收系统的设计与实现。项目旨在通过构建一个可靠、安全、透明的平台,提高废旧电子产品的回收率,推动资源的合理再利用,提供安全可靠的交易平台,加强环保意识,促进二手市场的发展,并实现数据驱动的智能化服务。项目面临的主要挑战包括废旧电子产品的检测与评估、信息不对称与交易风险、市场需求的预测与定价、用户体验优化及平台的安全性与数据保护。解决方案涵盖智能化评估与回收定价、高效的二手产品处理流程、完善的售后保障体系、创新的市场需求分析、全程透明化与安全性保障以及定制化用户体验。系统采用微服务架构,包括用户管理、商品评估、交易管理、数据分析、支付与结算等模块。项目还涉及前端界面设计、API接口开发、数据库设计与实现、模型训练与优化、部署与应用等方面。 适合人群:具备一定编程基础,特别是对Python和Web开发有一定了解的研发人员,以及对二手电子产品回收和环保事业感兴趣的从业者。 使用场景及目标:①帮助用户方便地将闲置电子产品回收、交易或再利用,提高废旧电子产品的回收率;②通过智能化的数据分析为用户提供价格评估、市场需求分析等服务,提高回收效率;③提供安全可靠的交易平台,确保交易的公平性和安全性;④推动二手市场的健康发展,为消费者提供经济实惠的产品选择;⑤增强公众的环保意识,推动社会向绿色、低碳方向发展。 其他说明:本文档不仅提供了系统的功能模块设计、数据库表结构、API接口规范,还展示了具体代码实现和GUI界面设计,为开发者提供了全面的技术参考。此外,项目强调了数据安全和隐私保护的重要性,确保平台在运行过程中能够有效保护用户信息。项目未来改进方向包括增强模型的精准度、拓展国际市场、提供更多支付和融资选项、跨平台数据集成与分析、更加智能的回收流程以及强化社交化与社区功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值