HDU 2612 (2次BFS,有点小细节)
Problem Description
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Y.#@
....
.#..
@..M Y.#@
....
.#..
@#.M Y..@.
.#...
.#...
@..M.
#...#
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#define min(x,y) ((x)<(y)? (x):(y))
using namespace std; const int INF=0x3f3f3f3f;
const int maxn=;
char Map[maxn][maxn];
int d1[maxn][maxn], d2[maxn][maxn];
int dx[]={,,,-}, dy[]={,-,,}; int main ()
{
//freopen("in.txt", "r", stdin);
int n,m;
while(cin>>n>>m)
{
int x1,y1,x2,y2;
for(int i=; i<maxn; i++)
for(int j=; j<maxn; j++)
{
d1[i][j]=d2[i][j]=;
} for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
cin>>Map[i][j];
if(Map[i][j]=='Y')
x1=i, y1=j;
if(Map[i][j]=='M')
x2=i, y2=j;
} queue<pair<int, int> > que;
pair<int, int> p;
p.first=x1, p.second=y1;
que.push(p);
while(que.size())
{
p=que.front(); que.pop();
for(int i=; i<; i++)
{
int nx=p.first+dx[i], ny=p.second+dy[i];
if( nx>=&&nx<n&&ny>=&&ny<m && (Map[nx][ny]=='.'||Map[nx][ny]=='@') && d1[nx][ny]==)
{
que.push(make_pair(nx,ny));
d1[nx][ny]=d1[p.first][p.second]+;
}
}
} p.first=x2, p.second=y2;
que.push(p);
while(que.size())
{
p=que.front(), que.pop();
for(int i=; i<; i++)
{
int nx=p.first+dx[i], ny=p.second+dy[i];
if( nx>=&&nx<n&&ny>=&&ny<m && (Map[nx][ny]=='.'||Map[nx][ny]=='@') && d2[nx][ny]==)
{
que.push(make_pair(nx,ny));
d2[nx][ny]=d2[p.first][p.second]+;
}
}
} int ans=INF;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(Map[i][j]=='@')
{
if(d1[i][j]!=&&d2[i][j]!=)
//这个判断之前没有想到,一直wa,后来看了下discuss,才发现这个问题,
//发现这个问题后,我最先d初始化为0改成初始化d为INF=0x3f3f3f3f,会溢出输出负数,
//发现自己很sb,算dis,d肯定初始化为0呀...,我看到这个min(d1[i][j]+d2[i][j], ans)有问题,就想到初始化为INF......
ans=min(d1[i][j]+d2[i][j], ans);
}
cout<<ans*<<endl;
}
return ;
}
HDU 2612 (2次BFS,有点小细节)的更多相关文章
- hdu 2612 多终点BFS
Find a way Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
- BFS(最短路) HDU 2612 Find a way
题目传送门 /* BFS:和UVA_11624差不多,本题就是分别求两个点到KFC的最短路,然后相加求最小值 */ /***************************************** ...
- HDU 2612 Find a way(双向bfs)
题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...
- HDU 2612 Find a way(找条路)
HDU 2612 Find a way(找条路) 00 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- Oracle Sales Cloud:管理沙盒(定制化)小细节2——使用对象触发器更新数字字段
在上一篇 "管理沙盒(定制化)小细节1" 的随笔中,我们使用公式法在 "业务机会" 对象(单头)上建立了 "利润合计" 字段,并将它等于 & ...
- Oracle Sales Cloud:管理沙盒(定制化)小细节1——利用公式创建字段并显示在前端页面
Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的CRM管理系统.由于 Oracle 销售云是基于 Oracle 云环境的,它与传统的管理系统相比,显著特点之一便 ...
- Oracle Sales Cloud:报告和分析(BIEE)小细节2——利用变量和过滤器传参(例如,根据提示展示不同部门的数据)
在上一篇随笔中,我们建立了部门和子部门的双提示,并将部门和子部门做了关联.那么,本篇随笔我们重点介绍利用建好的双提示进行传参. 在操作之前,我们来看一个报告和分析的具体需求: [1] 两个有关联的提示 ...
- Oracle Sales Cloud:报告和分析(BIEE)小细节1——创建双提示并建立关联(例如,部门和子部门提示)
Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的客户商机管理系统,通过提供丰富的功能来帮助提高销售效率,更好地去了解客户,发现和追踪商机,为最终的销售成交 (d ...
随机推荐
- python语法_注释
#加需要注释的内容,#号后面的单行注释 #这一段注释 左右各三个注释单引号或者双引号 中间的内容为注释,可以包含多行 '''这一段注释''' """这一段注释" ...
- Web开发——HTML DOM基础
文档资料参考: 参考:HTML DOM 参考手册 参考:HTML DOM 教程 目录: 1.HTML DOM (文档对象模型) 2.查找 HTML 元素 2.1 通过 id 查找 HTML 元素 2. ...
- [efficiency] emacs入门
一. 没记错的话,这可能是第三次读emacs tutorial了.前两次读的非常慢,也不记得有没有读完了.总之最后都忘光了. 这次读的很顺畅,利用工作的空闲时间加上今天晚上(周日).总算是读完了. 没 ...
- Linux学习5-CentOS安装Python3.6环境和pip3
前言 centos7 自带有 python,但是却是 python2 版本的 python,如果你想安装个python3怎么办呢? 如果直接删除python2的话,可能会引起其他的问题,因为有些东西是 ...
- kickstart ---无人值守安装
kickstart 实战 http://man.linuxde.net/download/CentOS_6_9镜像 安装: 关闭防火墙 且在编辑虚拟网络的vnet8中要取消使用本地dhcp项 1.yu ...
- Elasticsearch学习笔记(六)核心概念和分片shard机制
一.核心概念 1.近实时(Near Realtime NRT) (1)从写入数据到数据可以被搜索到有一个小延迟(大概1秒): (2)基于es执行搜索和分析可以达到秒级 2.集群(Cluster) 一个 ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- InternalError (see above for traceback): Blas GEMM launch failed
训练BiLSTM模型的时候报错: InternalError (see above for traceback): Blas GEMM launch failed : a.shape=(32, 200 ...
- C#设计模式(10)——组合模式(Composite Pattern)(转)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- tp 内置压缩文件zip
一.解压缩zip文件 $zip = new ZipArchive;//新建一个ZipArchive的对象 /* 通过ZipArchive的对象处理zip文件 $zip->open这个方法的参数表 ...