题目链接: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. VSS配置

    http://www.cnblogs.com/FreeDong/articles/2193151.html

  2. JavaWeb:JSP技术基础

    JavaWeb:JSP技术 快速开始 介绍 JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%> ...

  3. MCP|LDY|Mass Spectrometry-based Absolute Quantification of 20S Proteasome Status for Controlled Ex-vivo Expansion of Human Adipose-derived Mesenchymal Stromal/Stem Cells(基于质谱技术的20S蛋白酶体绝对定量方法监控人体脂肪...

    期刊名:Mol Cell Proteomics 发表时间:(2019年4月) IF:5.232   概述 20S蛋白酶体是一种多亚基蛋白质复合物,参与许多组织细胞生命活动过程.本研究基于SILAC标记 ...

  4. 【常见Web应用安全问题】

    Web应用程序的安全性问题依其存在的形势划分,种类繁多,这里不准备介绍所有的,只介绍常见的一些. 常见Web应用安全问题安全性问题的列表: 1.跨站脚本攻击(CSS or XSS, Cross Sit ...

  5. dorado 常用

    如果要设置模糊查询, 一般要在QueryCommand中这样写: var name = dsQuery.getValue("NAME"); var parameters = com ...

  6. 关于after和before

    你可曾'百度一下'? 在以前的很多时候,当我断网了,或者网络出了莫名其妙的问题时,我总是第一个输入它的网址.它不仅仅是一个搜索引擎.它还是我检验网络的唯一标准(手动滑稽). CSS中的after和be ...

  7. linux 文件权限除了r、w、x外还有s、t、i、a权限说明

    linux 文件权限除了r.w.x外还有s.t.i.a权限 s: 文件属主和组设置SUID和GUID,文件在被设置了s权限后将以root身份执行.在设置s权限时文件属主.属组必须先设置相应的x权限,否 ...

  8. Spring学习(五)事务管理

    Spring 事务管理: 一.事务概念: 1.什么是事务? 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消.也就是事务具有原子性,一个事务中的一系列的 ...

  9. SQLServer 索引的使用情况

    —在优化查询SQL语句,查看索引使用情况SQL语句: select db_name(database_id) as N'数据库名称', object_name(a.object_id) as N'表名 ...

  10. 图片单独上传 (word)

    Word.ApplicationClass wordApp=new ApplicationClass(); object file=path; object nullobj=System.Reflec ...