题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 , 一道比较简单的广搜(BFS)题目。


算法:

  设置两个dist[][]数组,记录Y和M到几个KFC的距离,最后求两个dist的和的最小值即可。

  还有,Y是可以走M的位置的,同理,M也可以走Y的位置,不用纠结这个问题。有一点要注意的就是有的KFC是Y或M无法到达的,所以在最后求最小值的时候要注意这个KFC是否访问过。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF 1e8
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
struct Pos {
int x , y;
};
Pos dir[] = {{- , } , { , } , { , } , { , -}};
vector <Pos> kfc;
int dist1[maxn][maxn] , dist2[maxn][maxn];
int vis[maxn][maxn] , dist[maxn][maxn];
char map[maxn][maxn];
int n , m; bool place(Pos a)
{
if(a.x < || a.y < || a.x >= n || a.y >= m || map[a.x][a.y] == '#' || vis[a.x][a.y])
return false;
return true;
}
void BFS(Pos start , int a[][maxn])
{
memset(vis , , sizeof(vis));
queue <Pos> que;
que.push(start);
vis[start.x][start.y] = ;
dist[start.x][start.y] = ;
while(!que.empty()) {
Pos u = que.front();
que.pop();
for(int i = ; i < ; i++) {
Pos tmp;
tmp.x = u.x + dir[i].x;
tmp.y = u.y + dir[i].y;
if(!place(tmp))
continue;
else {
que.push(tmp);
vis[tmp.x][tmp.y] = ;
dist[tmp.x][tmp.y] = dist[u.x][u.y] + ;
if(map[tmp.x][tmp.y] == '@')
a[tmp.x][tmp.y] = dist[tmp.x][tmp.y];
}
}
}
}
int main()
{
int i , j;
Pos start , end;
char ch;
while(~scanf("%d %d" , &n , &m))
{
memset(dist1 , , sizeof(dist1));
memset(dist2 , , sizeof(dist2));
kfc.clear();
for(i = ; i < n ; i++)
scanf("%s" , map[i]);
for(i = ; i < n ; i++) {
for(j = ; j < m ; j++) {
if(map[i][j] == 'Y') {
start.x = i;
start.y = j;
} else if(map[i][j] == 'M') {
end.x = i;
end.y = j;
} else if(map[i][j] == '@') {
Pos tmp = {i , j};
kfc.push_back(tmp);
}
}
}
BFS(start , dist1);
BFS(end , dist2);
int Min = INF;
for(i = ; i < kfc.size() ; i++) {
int x = kfc[i].x;
int y = kfc[i].y;
if(Min > dist1[x][y] + dist2[x][y] && dist1[x][y] && dist2[x][y])
Min = dist1[x][y] + dist2[x][y];
}
printf("%d\n" , * Min);
}
return ;
}

HDU2612 BFS的更多相关文章

  1. hdu2612(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意:求2个点到任意一个KFC的距离之和,使其最小. 分析:由两个点出发分别两次bfs,求得到每 ...

  2. hdu2612 Find a way BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路: 裸的BFS,对于Y,M分别进行BFS,求出其分别到达各个点的最小时间: 然后对于@的点, ...

  3. 简单bfs(hdu2612)

    #include<stdio.h>#include<string.h>#include<queue>#define INF 0x3f3f3f3fusing name ...

  4. HDU-2612.Find way .(不同起点不同终点的BFS)

    我要被这个好用的memset气死了...... 真香 #include <cstring> #include <string> int main () { ]; memset( ...

  5. HDU2612 Find a way —— BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others ...

  6. HDU2612 Find a way (跑两遍BFS)

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...

  7. hdu 2612 Find a way(BFS)

    题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...

  8. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  9. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

随机推荐

  1. C# 写 LeetCode easy #27 Remove Element

    27. Remove Element Given an array nums and a value val, remove all instances of that value in-place  ...

  2. php5.6安装window7安装memcache.dll库所遇到的误区

    问题: window7 64位,下载的库 memcache.dll 为64位的,且对应php的版本.但是重启后phpstudy查看phpinfo依然没有memcache: 根源: 发现是下载的 mem ...

  3. Python爬虫开发

    1. 语法入门 Python教程 2. 爬虫学习系列教程 1)宁哥的小站 https://github.com/lining0806/PythonSpiderNotes 2)Python爬虫开发 3) ...

  4. 洛谷P1912 [NOI2009]诗人小G(决策单调性)

    传送门 题解 决策单调性是个啥……导函数是个啥……这题解讲的是啥……我是个啥…… //minamoto #include<iostream> #include<cstdio> ...

  5. centos 基础设置

    centos 6 关闭防火墙 查看防火墙是否开启 service iptables status 停止防火墙 service iptables stop 禁止开机自启动防火墙 chkconfig ip ...

  6. 5.Python初窥门径(字典)

    Python字典学习 1.字典初识 ​ 1.字典的简单介绍 ​ 字典(dict),是python中唯一的映射类型.他是以{ }括起来的键值对组成.在dict中key是唯一的.在保存的时候,根据key来 ...

  7. File类、递归

    File类.递归 1.1File类概述 java.io.File表示文件和目录路径名的抽象类.可以对文件和文件夹进行创建删除.获取.判断.遍历等功能. 1.2路径\文件分割符,相对路径绝度路径 1.2 ...

  8. 《SQL 进阶教程》 自连接排序

    子查询所做的,是计算出价格比自己高的记录的条数并将其作为自己的位次 -- 自连接实现排序功能SELECT P1.name,P1.price,(SELECT COUNT(P2.price)FROM Pr ...

  9. vbox和宿主机共享文件夹

    首先,查看vbox安装的ubuntu是否支持vboxsf模块 sudo modprobe vboxsf dmesg | grep vboxsf 如果没有安装,需要安装vboxsf模块:(如果安装了请跳 ...

  10. 双层列表 datagrid里属性

    frozenColumns: [ [{ title: "姓名"}] ], columns: [ [{"title":"延时原因"}], [{ ...