题目链接: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. js方法调用

    <!DOCTYPE html> <html> <head> <title>测试</title> </head> <body ...

  2. CSS 定位元素之 relative

    1. relative 和 absolute relative 会限制 absolute. absolute 会根据 父级的的定位元素来定位. 2. overflow 和 absolue 当overf ...

  3. docke 网络配置2

    一,docker 的bridge模式是和vmware中的nat模式类似的,但是如果想要弄成和vmwae中的bridge怎么办呢? 说明,bridge模式获取的Ip是与宿主机的ip是出于同一个网段的. ...

  4. DEV GridControl 鼠标单击事件

    private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { ...

  5. post请求和get请求的区别

    1:如果表单是以post方式发送,那么表单中的数据会放在请求报文体中,发送到服务端.但是如果是以get方式提交表单,那么表单中用户输入的数据都是以URL地址的方式发送到服务端. 2:在服务端接收数据时 ...

  6. (转)一小时搞定DIV+CSS布局-固定页面开度布局

    本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...

  7. ajaxFileUpload js判断类型

    function ajaxFileUpload() {    var File_box = document.getElementById('V_file');    var extend = Fil ...

  8. JAVA与JSON的序列化、反序列化

    package com.linkage.app.memcache; import java.util.HashMap;import java.util.Map.Entry; import net.sf ...

  9. 通过 sp_configure 进行 Database Mail 配置

    通过 sp_configure 进行 Database Mail 配置 直接执行步骤一. 如果报错,则先执行步骤二,再执行步骤一. 一. sp_configre ; GO RECONFIGURE; G ...

  10. call()与apply()传参需要注意的一点

    call()与apply()是用来改变函数体内的this指向,第一个参数是调用函数的母对象,他是调用上下文,函数体内通过this来获得对它的引用,换句话说就是第一参数===函数中的this. 但是如下 ...