HDU2612 Find a way (跑两遍BFS)
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.
InputThe input contains multiple test cases.
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
OutputFor each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
注意几个地方:1.必须两个人都要访问到那个肯德基才能用它更新答案。2.Y,M,肯德基都可以经过。3.搜到肯德基以后万万不能直接continue,因为肯德基能经过。万一有
..Y.
##@#
##@#
..M.这样的数据就会WA!!没注意到这里卡了一下午QnQ
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x;
int y;
int time;
};
int n,m;
char mmap[][];
int ans[][][];
bool vis[][];
const int INF=0x3f3f3f3f; int dir[][]={{,},{-,},{,},{,-}};
void bfs(int p,int sx,int sy)//p为0:Y p为1:M sx、sy为起点坐标
{
queue<node>q;
node start;
start.x=sx,start.y=sy,start.time=;
q.push(start);
vis[sx][sy]=;
while(q.size())
{
node pre;
pre=q.front();
q.pop();
if(mmap[pre.x][pre.y]=='@')
{
ans[pre.x][pre.y][p]=pre.time*;// p为0:Y花的时间 p为1:M花的时间
// continue;千万不能写continue!!!!
}
int i;
for(i=;i<=;i++)
{
int nx=pre.x+dir[i][];
int ny=pre.y+dir[i][];
if(!vis[nx][ny]&&nx>=&&nx<n&&ny>=&&ny<m&&mmap[nx][ny]!='#')
{
node nxt;
nxt.x=nx;
nxt.y=ny;
nxt.time=pre.time+;
q.push(nxt);
vis[nx][ny]=;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(ans,,sizeof(ans));
memset(mmap,,sizeof(mmap));
memset(vis,,sizeof(vis));
int i,j;
for(i=;i<n;i++)
{
scanf("%s",mmap[i]);
}
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(mmap[i][j]=='Y')
{
memset(vis,,sizeof(vis));
bfs(,i,j);
}
else if(mmap[i][j]=='M')
{
memset(vis,,sizeof(vis));
bfs(,i,j);
}
}
}
int out=INF;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(ans[i][j][]!=&&ans[i][j][]!=)out=min(out,ans[i][j][]+ans[i][j][]);//必须要两个人都能到kfc
}
}
cout<<out<<endl;
}
}
HDU2612 Find a way (跑两遍BFS)的更多相关文章
- Magic Potion(最大流,跑两遍网络流或者加一个中转点)
Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...
- 求树的直径【两遍BFS】
两遍BFS.从任意一个点出发,第一遍可以找到直径的一端,从这端出发即可找到另外一端. 证明:从U点出发,到达V[画个图便清晰了] 1.如果U在直径上,则V一定是直径的一个端点. 2.如果U不在直径上. ...
- 洛谷P1073最优贸易(跑两遍dij)
题目描述 CC C国有n n n个大城市和m mm 条道路,每条道路连接这 nnn个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 mmm 条道路中有一部分为单向通行的道路,一部分为 ...
- POJ3268 Silver Cow Party (建反图跑两遍Dij)
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...
- CSU - 2031 Barareh on Fire (两层bfs)
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...
- P3119 [USACO15JAN]草鉴定Grass Cownoisseur 分层图或者跑两次最长路
https://www.luogu.org/problemnew/show/P3119 题意 有一个有向图,允许最多走一次逆向的路,问从1再走回1,最多能经过几个点. 思路 (一)首先先缩点.自己在缩 ...
- 系统警告,Bronya请求支援(两遍最短路)
系统警告,Bronya请求支援 Description 休伯利安号的一行人来到了由逆熵镇守的前文明遗迹[海渊城],他们准备用巨大的传送装置[海渊之眼]进入量子之海,寻找丢失的渴望宝石.然而在行动前夜爱 ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- HDU5886 Tower Defence 【两遍树形dp】【最长链预处理】
题意:N个点的一棵带权树.切掉某条边的价值为切后两树直径中的最大值.求各个边切掉后的价值和(共N-1项). 解法一: 强行两遍dp,思路繁琐,维护东西较多: dis表示以i为根的子树的直径,dis2表 ...
随机推荐
- [lua]紫猫lua教程-命令宝典-L1-01-12. 临时补充2
1.lua的环境变量和函数 (1)_G表 (个人习惯遍历下_G 看看当前环境支持什么库 很多库不是默认就支持的 需要按照流程导入或者加载) 一个全局变量(非函数),内部储存有当前所有的全局函数和全局 ...
- 牛客多校第二场H Second Large Rectangle 单调栈or悬线法
Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...
- HTML学习(3)基础
*开始标签常被称为起始标签(opening tag),结束标签常称为闭合标签(closing tag). *虽然有时候忘记写结束标签有的浏览器也能正常显示内容,但有时候会出现不可预料的结果或错误,所以 ...
- 使用Id访问table对象,使用Id访问Input对象
先看例子(好吧 无意中发现 可以通过Id访问DOM元素,如div) <!DOCTYPE html> <html> <head> <meta cha ...
- Vue - 过渡 列表过渡
列表的进入/离开过渡 获取不大于数组长度的随机数,作为插入新值的位置 <div id="app" class="demo"> <button ...
- AcWing 859. Kruskal算法求最小生成树 稠密图
//稠密图 #include <cstring> #include <iostream> #include <algorithm> using namespace ...
- Bug搬运工-CSCvg37458:ISR4K goes into booting loop with "flash:" in boot statement
ISR4K升级的时候要注意了! 很可能会碰到如下的问题: ISR4K goes into booting loop with "flash:" in boot statement ...
- FileOutputStream,BufferedOutputStream,FileWriter 效率比较
测试代码: /** * 写文件 * FileOutputStream, BufferedOutputStream, FileWriter * 三个流 效率比较 */ @Test public void ...
- BFSDFS模板
BFS模板: private static void bfs(HashMap<Character, LinkedList<Character>> graph,HashMap&l ...
- 基于Aspectj表达式配置的Spring AOP
AOP(Aspect-Oriented Programming, 面向切面编程):是一种新的方法论, 是对传统OOP(Object-Oriented Programming, 面向对象编程)的补充. ...