hdu Find a way
算法:广搜;
Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
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.
Input
The 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
Output
For 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
啥也不说了直接代码
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 10000000
int n,m,c[205][205],g[4][2]={-1,0,1,0,0,-1,0,1},e[205][205],a[205][205],b[205][205];
char ch[205][205];
struct dot
{
int x, y;
} ;
void bfs(int dx,int dy,int k)
{ memset(e,0,sizeof(e));
queue<dot>que;
dot cur,loer;
cur.x=dx;cur.y=dy;
if(k==0)a[dx][dy]=0;
else b[dx][dy]=0;
que.push(cur);
while(que.size())
{
loer=que.front();
que.pop();
for(int i=0;i<4;i++)
{
int nx=loer.x+g[i][0];
int ny=loer.y+g[i][1];
if(nx>=0&&nx<n&&ny>=0&&ny<m&&ch[nx][ny]!='#'&&!e[nx][ny])
{
if(k==0)a[nx][ny]=a[loer.x][loer.y]+1;
else if(k==1)b[nx][ny]=b[loer.x][loer.y]+1;
cur.x=nx;
cur.y=ny;
e[nx][ny]=1;
que.push(cur);
}
}
}
}
int main()
{
int i,j,k,p,q,s,t;
while(cin>>n>>m)
{
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>ch[i][j];
a[i][j]=b[i][j]=INF;
if(ch[i][j]=='Y')
{
s=i;t=j;ch[i][j]='#';
}
else if(ch[i][j]=='M')
{
p=i;q=j;ch[i][j]='#';
}
}
}
bfs(s,t,0);
bfs(p,q,1);
int Mx=10000000;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ch[i][j]=='@')
{
if(Mx>a[i][j]+b[i][j])
Mx=a[i][j]+b[i][j];
}
}
}
cout<<11*Mx<<endl;
}
return 0;
}
hdu Find a way的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
- HDU 2586
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:求最近祖先节点的权值和 思路:LCA Tarjan算法 #include <stdio.h&g ...
随机推荐
- 【转】【C/C++】内存分配函数:malloc,calloc,realloc,_alloca
转自:http://www.cnblogs.com/particle/archive/2012/09/01/2667034.html#commentform malloc: 原型:extern voi ...
- 微信支付:redirect_uri参数错误 的解决办法
redirect_url参数错误: 报这个错误,说明你的公众号后台授权设置有问题(一般有两处) 一:检查授权目录 答:支付授权目录是网站发起请求的页面所在目录,并且必须是能通过url地址访问的(与真实 ...
- Python入门-----Windows安装
摘要:Python,windows安装 1.进入python的官方网站下载:https://www.python.org 点击Download,选择windows版本:
- 从现在开始使用nodejs开发的几点答疑
1.为什么要开始用nodejs, 而不是php 理由有三点: 因为我是前端程序员出身,nodejs都是用javascript写的,这样相当于前端和后端都使用javascript,开发更加有效率.当然很 ...
- FLASH驱动之-块设备驱动系统构架
一. 块设备是只能以块为单位进行访问的设备,块的大小一般是512个字节的整数倍,常见的块设备包括硬件,SD卡,光盘,flash等.驱动程序是块的整数倍从设备读写得到数据.块设备的最小访单位为块,不同 ...
- LeetCode_Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- Qt多国语言QT_TR_NOOP和QT_TRANSLATE_NOOP
文章来源:http://devbean.blog.51cto.com/448512/245063/ 在代码中,我们使用tr()将需要翻译的字符串标记出来.lupdate工具就是提取出tr()函数中的相 ...
- CH Round #53 -密室
描述 有N个密室,3种钥匙(红色,绿色,白色)和2种锁(红色,绿色),红色钥匙只能开红色的锁,绿色钥匙只能开绿色的锁,白色钥匙可以开红色的锁和绿 色的锁,一把钥匙使用一次之后会被扔掉.每个密室由一扇门 ...
- 图论:2-SAT模板
#include<cstdio> #include<vector> #include<cstring> using namespace std; const int ...
- php字符串常用处理函数(数组的拆分、查找替换)
//字符串常用函数 $a = "hello"; echo strlen($a); //输出字符串的长度 $b = "Hello"; ...