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 ...
随机推荐
- js方法调用
<!DOCTYPE html> <html> <head> <title>测试</title> </head> <body ...
- Hacker(18)----了解Windows系统漏洞
一.WinXP中的漏洞 在WinXP中,常见的漏洞主要有UPNP服务漏洞.帮助与支持中心漏洞.压缩文件夹漏洞.服务拒绝漏洞.RDP漏洞以及热键漏洞. 1.UPNP服务漏洞 漏洞描述:UPNP(Univ ...
- _js day10
- WebApi2官网学习记录---异常处理
HttpResponseException 当WebAPI的控制器抛出一个未捕获的异常时,默认情况下,大多数异常被转为status code为500的http response即服务端错误. Http ...
- compilation 元素(ASP.NET 设置架构)
配置 ASP.NET 用于编译应用程序的所有编译设置. <configuration> 元素 system.web 元素(ASP.NET 设置架构) compilation 元素( ...
- ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
转帖:原文地址http://blog.csdn.net/panys/article/details/3838846 archive log 日志已满ORA-00257: archiver error. ...
- tableview 分割线置最左边的解决方法
首先在viewDidLoad方法加入以下代码: if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [se ...
- JS控制文本框textarea输入字数限制的方法
<html> <head runat="server"> <title></title> <script type=" ...
- DOM 节点属性
DOM 节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeT ...
- [Mugeda HTML5技术教程之18]如何在Android应用中使用Mugeda动画内容
1.简介 本文主要介绍如何在Android应用程序中使用Mugeda动画.Mgeda动画是标准HTML5格式的动画,在Android应用程序中可以使用WebView来加载Mugeda动画.动画内容本身 ...