HDU2612 BFS
题目链接: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的更多相关文章
- hdu2612(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意:求2个点到任意一个KFC的距离之和,使其最小. 分析:由两个点出发分别两次bfs,求得到每 ...
- hdu2612 Find a way BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路: 裸的BFS,对于Y,M分别进行BFS,求出其分别到达各个点的最小时间: 然后对于@的点, ...
- 简单bfs(hdu2612)
#include<stdio.h>#include<string.h>#include<queue>#define INF 0x3f3f3f3fusing name ...
- HDU-2612.Find way .(不同起点不同终点的BFS)
我要被这个好用的memset气死了...... 真香 #include <cstring> #include <string> int main () { ]; memset( ...
- HDU2612 Find a way —— BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 MS (Java/Others ...
- HDU2612 Find a way (跑两遍BFS)
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...
- hdu 2612 Find a way(BFS)
题目链接:hdu2612 思路:题意是求两个人到某一个KFC花费时间和最小,其实就是求最短距离和,用两个BFS,分别以两个人为起点,分别记录下两人到每个KFC的距离,然后求出最小的和 #include ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
随机推荐
- 【转】log4j.properties 详解与配置步骤 - edward0830ly的专栏 - 博客频道 - CSDN.NET
一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...
- Unity中限制轴向移动范围Mathf.Clamp
Mathf.Clamp 在游戏中,为了限制玩家的某一轴向的移动不超过一定的范围,可以用Mathf.Clamp来解决 Mathf.Clamp(float value,float min,float ...
- [Linux]关于sigprocmask函数的讨论
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...
- 中山纪念中学20170310洗衣服(贪心,优先队列升序【pair】)
#include<bits/stdc++.h>using namespace std;typedef pair<long long,int>clot;priority_queu ...
- Java8 使用 stream().map()提取List对象的某一列值及排重
List对象类(StudentInfo) public class StudentInfo implements Comparable<StudentInfo> { //名称 privat ...
- Unity3D -- shader光照常用函数和变量
上一篇记录了shader常用函数和变量,这篇记录一些光照计算时常用函数和变量 1.内置的光照变量 _LightColor0 float4 //该Pass处理的逐像素光源的颜色 _WorldSpaceL ...
- DataGridView控件对Column的设置
http://stackoverflow.com/questions/18666582/datagridview-autofit-and-fill 1.Column覆盖所有width: dgv.Aut ...
- bzoj3876: [Ahoi2014&Jsoi2014]支线剧情(上下界费用流)
传送门 一道题让我又要学可行流又要学zkw费用流…… 考虑一下,原题可以转化为一个有向图,每次走一条路径,把每一条边都至少覆盖一次,求最小代价 因为一条边每走过一次,就要付出一次代价 那不就是费用流了 ...
- dorado 常用
如果要设置模糊查询, 一般要在QueryCommand中这样写: var name = dsQuery.getValue("NAME"); var parameters = com ...
- express-http-proxy 的基础使用
const app = express() app.use(matchPath, proxy(serverAddress, { proxyReqPathResolver: function(req) ...