题目链接: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)的更多相关文章

  1. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

  2. HDU - 2612 Find a way(BFS搜索)

    题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...

  3. 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 ...

  4. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  5. 题解报告:hdu 2717 Catch That Cow(bfs)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  6. 【HDU - 2102】A计划(bfs)

    -->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...

  7. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

  8. 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 ...

  9. HDU 1180 诡异的楼梯(BFS)

    诡异的楼梯 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status ...

随机推荐

  1. C语言的学习-基础知识点

    ---BOOL BOOL BOOL a = YES; printf("%d\n", a); a = NO; printf("%d", a); , b = ; B ...

  2. C#控制台吹泡泡算法

    代码如下: static void Main(string[] args) { Bubbling(100, 100, "O", 1000); Console.ReadLine(); ...

  3. LDAP禁止匿名访问

    LDAP默认是允许用户匿名访问的,如下图:在使用工具连接时,勾选匿名绑定后,不需要输入UserDN和密码就可能连接到LDAP服务器,但是只能进行read及search操作.不能做任何的修改及删除操作. ...

  4. CSS 实现三角形、梯形、等腰梯形

    三角形 ; width: 0px; border-width: 0px 30px 45px 145px; border-style: none solid solid; border-color: t ...

  5. Javascript进阶篇——(DOM—节点---获取浏览器窗口可视区域大小+获取网页尺寸)—笔记整理

    浏览器窗口可视区域大小获得浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)的方法:一.对于IE9+.Chrome.Firefox.Opera 以及 Safari: • window.innerH ...

  6. 《第一行代码》学习笔记3-活动Activity(1)

    1.活动-一种可以包含用户界面的组件,用于和用户进行交互. <Button android:id="@+id/button_1" android:layout_width=& ...

  7. js中的因数分解

    方法一: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...

  8. php环境安装及搭建

    最近由于项目需要 转战 PHP .  在做了差不多两年java后 说实话看php代码还是有些难受的. 毕竟不习惯.废话不说 先说一下 PHP环境的部署等等,也就是最近几天学习的心得吧.方便以后参考. ...

  9. python functools.wraps装饰器模块

    # -*-coding=utf-8 -*-#实现一个函数执行后计算执行时间的功能 __author__ = 'piay' import time, functools def foo(): ''' 定 ...

  10. C语言变量的存储类别

    我们知道,从变量的作用域(即从空间)角度来分,可以分为全局变量和局部变量. 从另一个角度,从变量值存在的作时间(即生存期)角度来分,可以分为静态存储方式和动态存储方式. 静态存储方式:是指在程序运行期 ...