hdu 2612 Find a way(BFS)
题目链接:hdu2612
思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 205
using namespace std;
char map[N][N];
int v[N][N],ans1[N][N],ans2[N][N],d[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
int x1,x2,y1,y2,n,m,num;
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step > b.step;
}
};
void bfs1(int x,int y,int total)//total表示KFC的总数目
{
memset(ans1,1,sizeof(ans1));
memset(v,0,sizeof(v));
priority_queue <node> q;
node s,temp;
s.x = x;
s.y = y;
s.step = 0;
v[x][y] = 1;
q.push(s);
while(!q.empty())
{
temp = q.top();
q.pop();
if(map[temp.x][temp.y] == '@')
{
ans1[temp.x][temp.y] = temp.step;
total -- ;
}
if(total == 0) return;//所有的KFC都已经计算过
for(int i= 0 ; i < 4 ; i ++)
{
s = temp;
s.x += d[i][0];
s.y += d[i][1];
if(s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || v[s.x][s.y] || map[s.x][s.y] == '#')
continue;
v[s.x][s.y] = 1;
s.step ++;
q.push(s);
}
}
}
void bfs2(int x,int y,int total)
{
memset(ans2,1,sizeof(ans2));
memset(v,0,sizeof(v));
priority_queue <node> q;
node s,temp;
s.x = x;
s.y = y;
s.step = 0;
v[x][y] = 1;
q.push(s);
while(!q.empty())
{
temp = q.top();
q.pop();
if(map[temp.x][temp.y] == '@')
{
ans2[temp.x][temp.y] = temp.step;
total -- ;
}
if(total == 0) return;
for(int i= 0 ; i < 4 ; i ++)
{
s = temp;
s.x += d[i][0];
s.y += d[i][1];
if(s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || v[s.x][s.y] || map[s.x][s.y] == '#')
continue;
v[s.x][s.y] = 1;
s.step ++;
q.push(s);
}
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
num = 0;
for(i = 0 ; i < n ; i ++)
{
scanf("%s",map[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;
}
else if(map[i][j] == '@')
num++;
}
}
bfs1(x1,y1,num);
bfs2(x2,y2,num);
int sum = 10000000;
for(i = 0 ; i < n ; i ++)
for(j = 0 ; j < m ; j ++)
if(map[i][j] == '@')
{
sum = min(sum,ans1[i][j] + ans2[i][j]);
}
printf("%d\n",sum * 11);
}
return 0;
}
hdu 2612 Find a way(BFS)的更多相关文章
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- HDU - 2612 Find a way(BFS搜索)
题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- 【HDU - 2102】A计划(bfs)
-->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- HDU 1180 诡异的楼梯(BFS)
诡异的楼梯 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
随机推荐
- git 学习笔记一
1.git的 介绍 分布式和 集中式 集中式版本控制系统最大的毛病就是必须联网才能工作,如果在局域网内还好,带宽够大,速度够快,可如果在互联网上,遇到网速慢的话,可能提交一个10M的文件就需要5分钟, ...
- CentOS6.6修改主机名和网络信息
1.修改主机名称 [root@centos ~]# vim /etc/sysconfig/network #打开文件,修改以下内容并保存 NETWORKING=yes #使用网络HOSTNAME=ce ...
- 虚拟现实,增强现实,VR,AR
现在的热点不止VR,还有AR和披着MR.HR.CR外衣的各种高级AR们,所以比较着一起说.以下知乎上一网友观点,放几条结论:1.近期(未来两三年)看,VR能火,AR尚待成熟: 2.VR设备中,插片式是 ...
- line-hight-(行高)解析
行高定义:line-height属性是指文本行基线之间的距离. 顶线.中线.基线.底线概念 从上到下四条线分别是顶线.中线.基线.底线,很像才学英语字母时的四线三格,我们知道vertical-alig ...
- C#操作Flash动画
对于在C#开发的过程中没有接触过Flash相关开发的人员来说,没有系统的资料进行学习,那么这篇文档针对于初学者来说是很好的学习DEMO. 本文章中的DEMO实现了C#的COM控件库中本来就带有对fla ...
- C# Excel 读取为Datatable
最近项目用到的读取Excel 为DataTable 兼容2003.2007.2010.记录一下,以后会用到 引用 NPOI.dll 和 EPPlus.dll using System; using S ...
- Java的一点内容(2)
1 面向对象的三个原则 封装性 封装的基本单元是类(class),类是一个抽象的逻辑结构,而类的对象是一个真实的物理实体:类的目的是封装复杂性,在类内部存在隐藏实现复杂性机制: 封装(encapsul ...
- MFC下的aero效果学习笔记
最近想在MFC中用下aero 首先参考了 http://blog.csdn.net/polytechnic/article/details/5696797 中的一系列的步骤,进行了初步学习 但是对于其 ...
- PAT 65. A+B and C (64bit) (20)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1065 思路分析: 1)对a+b造成的long long 类型的数据溢出进行特殊处理: a> ...
- canvas元素大小与绘图表面大小
原文链接:canvas总结:元素大小与绘图表面大小 前言 我们使用canvas的时候一般在canvas元素中直接设置它的width和height: 1 <canvas id="myCa ...