hdoj 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): 5401 Accepted Submission(s):
1823
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.
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
both yifenfei and Merceki to arrival one of KFC.You may sure there is always
have a KFC that can let them meet.
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 210
#define INF 0x3f3f3f
using namespace std;
int bu1[MAX][MAX];//记录第一个人步数
int bu2[MAX][MAX];//记录第二个人步数
int p;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m;
struct node
{
int x,y;
int step;
};
int MIN(int x,int y)
{
return x<y?x:y;
}
void bfs(int x1,int y1,int p)
{
memset(vis,0,sizeof(vis));
int j,i,ok=0;
int move[4][2]={0,1,0,-1,1,0,-1,0};
queue<node>q;
node beg,end;
beg.x=x1;
beg.y=y1;
beg.step=0;
q.push(beg);
while(!q.empty())
{
end=q.front();
q.pop();
if(map[end.x][end.y]=='@')//遇见@则表示到达终点
{
if(p==1)
bu1[end.x][end.y]=end.step;
else
bu2[end.x][end.y]=end.step;
}
for(i=0;i<4;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
if(!vis[beg.x][beg.y]&&0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m&&map[beg.x][beg.y]!='#')
{
vis[beg.x][beg.y]=1;
beg.step=end.step+11;
q.push(beg);
}
}
}
}
int main()
{
int sum,j,i,t,k,x1,x2,y1,y2,min;
int s[11000];
while(scanf("%d%d",&n,&m)!=EOF)
{ for(i=0;i<n;i++)
{
scanf("%s",map[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
x1=i;y1=j;
}
else if(map[i][j]=='M')
{
x2=i;y2=j;
}
}
}
memset(bu1,INF,sizeof(bu1));
bfs(x1,y1,1);
memset(bu2,INF,sizeof(bu2));
bfs(x2,y2,2);
min=INF;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(bu1[i][j]!=INF&&bu2[i][j]!=INF)
{
min=MIN(bu1[i][j]+bu2[i][j],min);//取两者步数和的最小值
}
}
}
printf("%d\n",min);
}
return 0;
}
hdoj 2612 Find a way【bfs+队列】的更多相关文章
- HDU/HDOJ 2612 Find a way 双向BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路:从两个起点出发,有多个终点,求从两个起点同时能到达的终点具有的最小时间,开两个数组分别保存 ...
- HDU 2612 - Find a way - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Problem DescriptionPass a year learning in Hangz ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- BFS 队列
Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants to model thi ...
- 农夫过河 (BFS)(队列)
1 .问题描述 要求设计实现农夫过河问题(农夫带着一只狼,一只养,一棵白菜,一次只能带一个东西)如何安全过河. 2 .问题的解决方案: 可以用栈与队列.深度优先搜索算法及广度优先搜索算法相应的原理去解 ...
- 拆边+BFS队列骚操作——cf1209F
这个拆边+队列操作实在是太秒了 队列头结点存的是一个存点集的vector,1到这个点集经过的路径权值是一样的,所以向下一层拓展时,先依次走一遍每个点的0边,再走1边...以此类推,能保证最后走出来的路 ...
- POJ——3278 Catch That Cow(BFS队列)
相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...
随机推荐
- 项目中常用功能,如:流媒体、健康数据(步数等)等-b
整理iOS开发中使用的各种流媒体和常用的高级功能.由于时间关系,目前只写了一部分功能,全部都采用的是系统方法,没用第三方,截图如下: screen1.png screen2.png 个人比较懒,不爱多 ...
- 28 个必备的 Linux 命令行工具
dstat & sar iostat, vmstat, ifstat and much more in one. slurm 网络流量图形化工具 vim & emacs 这个没人不知道 ...
- 使用Yeoman搭建 AngularJS 应用 (2) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/index.html 使用Yeoman搭建简单的应用 今天将会搭建一个简单的网页程序.你将可以添加,删除,拖拽和保存. 浏览Yeoman Y ...
- VS Extension: WPF : 使用全局方式 设置 窗体 foreground background
VS Extension 中 创建 WPF窗体后,总希望窗体的前后背景色和VS当前配色方案一致. 对每个control使用下列定义可以达到效果 Background="{DynamicRes ...
- [AC自动机]HDOJ3695 Computer Virus on Planet Pandora
题意:给t.n,t个案例,n个字符串 下面给n+1个字符串,n个互不相同的小串,最后一个是模式串 模式串会出现[qx]的形式,q为数字,x为一个字母 问n个小串在模式串中出现的个数,正着出现.反着出现 ...
- perl的输出缓冲
今天写一个小脚本的时候,需要即时输出当前进度到命令行上,并即时将重要数据写入报告文件中.但是perl默认是有输出缓冲的,显示到命令行上必须以\n结尾才行,输出到文件中,回车了都不行,非得要等缓冲区 ...
- HashMap与HashTable联系与区别
HashMap与HashTable 1.hashMap去掉了HashTable 的contains方法,但是加上了containsValue()和containsKey()方法. 2.hashTabl ...
- Bamboo简介
前言 前面介绍了JIRA管理平台,那么本篇就来介绍关于自动编译项目的工具-Bamboo. 正题 1.简介 Atlassian Bamboo 是一款持续集成构建服务器软件(Build Serve ...
- hdu4666Hyperspace
http://acm.hdu.edu.cn/showproblem.php?pid=4666 先看一个求曼哈顿的帖子http://www.cnblogs.com/lmnx/articles/24797 ...
- WPF——绑定数据库数据(Listview)
一.首先先画一个窗体,放进一个Listview 然后给每列起好名字,并且绑定的数据是临时表的列名 二.造一个临时表用来存储数据,并且将扔进去的Listview绑定到这个临时表DataTable上面 p ...