Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12795    Accepted Submission(s): 4105

Problem 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
 

Author

yifenfei
 

Source

 
 //2017-02-28
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; struct node{
int x, y, step;
void setNode(int x, int y, int step){
this->x = x;
this->y = y;
this->step = step;
}
};
char grid[][];
bool book[][];
int Ydis[][], Mdis[][];
int n, m;
int dx[] = {, , , -};
int dy[] = {, , -, };
const int inf = 0x3f3f3f3f; void bfs(int sx, int sy)
{
node tmp;
tmp.setNode(sx, sy, );
queue<node> q;
q.push(tmp);
memset(book, , sizeof(book));
book[sx][sy] = ;
int x, y, step;
while(!q.empty())
{
x = q.front().x;
y = q.front().y;
step = q.front().step;
q.pop();
if(grid[x][y] == '@'){
if(grid[sx][sy] == 'Y')Ydis[x][y] = step;
else Mdis[x][y] = step;
}
for(int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&!book[nx][ny]&&grid[nx][ny]!='#'){
book[nx][ny] = ;
tmp.setNode(nx, ny, step+);
q.push(tmp);
}
}
}
} int main()
{
while(cin>>n>>m)
{
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
cin>>grid[i][j];
memset(Ydis, inf, sizeof(Ydis));
memset(Mdis, inf, sizeof(Mdis));
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(grid[i][j] == 'Y' || grid[i][j] == 'M')
bfs(i, j);
int ans = inf;
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(grid[i][j] == '@' && ans > Ydis[i][j]+Mdis[i][j])
ans = Ydis[i][j] + Mdis[i][j];
cout<<*ans<<endl;
} return ;
}

HDU2612(KB1-N)的更多相关文章

  1. 简单bfs(hdu2612)

    #include<stdio.h>#include<string.h>#include<queue>#define INF 0x3f3f3f3fusing name ...

  2. hdu2612 Find a way

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  3. hdu2612.。。。

    原题链接 水了一天bfs了 题意:2个人分别从Y,M出发,到达其中任意一个“@” (图中有多个“@”点),2人到达的必须是同一个“@”点,求最短的路程和 思路:bfs搜2次,用一个2维数组记录到达各个 ...

  4. 暑假集训(1)第四弹 -----Find a way(Hdu2612)

    Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Nin ...

  5. hdu2612(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题意:求2个点到任意一个KFC的距离之和,使其最小. 分析:由两个点出发分别两次bfs,求得到每 ...

  6. hdu2612 Find a way BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路: 裸的BFS,对于Y,M分别进行BFS,求出其分别到达各个点的最小时间: 然后对于@的点, ...

  7. HDU-2612.Find way .(不同起点不同终点的BFS)

    我要被这个好用的memset气死了...... 真香 #include <cstring> #include <string> int main () { ]; memset( ...

  8. Hdu2612 Find a way 2017-01-18 14:52 59人阅读 评论(0) 收藏

    Find a way Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

  9. HDU2612 -暑假集训-搜索进阶N

     http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/N这两天总是因为一些小错误耽误时间,我希望自己可以细心点.珍惜 ...

随机推荐

  1. jzoj5848

    小 S 热爱大自然, 一天他种了一棵奇怪的线段树. 奇怪的线段树是一种与普通线段树类似的结构, 唯一不同的是, 它不一定以每一个区间的中点作为分治中心. 麻烦的是, 小 S 的线段树被风吹散了, 散成 ...

  2. MySQL(外键变种)

    day58 外键的变种                  a. 用户表和部门表                      用户:        不唯一                1 alex    ...

  3. zoj4016 Mergeable Stack

    题意:对n个栈,有q次操作.每个操作可能为三种情况中的一种:1.将v插入到s栈的顶端:2.输出s栈的栈顶(若栈为空则输出empty):3.将栈t插入到栈s的栈顶. 开始考虑到指针可能会mle,用数组模 ...

  4. .NET Windows Form 改变窗体类名(Class Name)有多难?

    研究WinForm的东西,是我的一个个人兴趣和爱好,以前做的项目,多与WinForm相关,然而这几年,项目都与WinForm没什么关系了,都转为ASP.NET MVC与WPF了.关于今天讨论的这个问题 ...

  5. webpack快速入门——Json配置文件使用

    在实际工作中,我们的项目都会配置一个Json的文件或者说API文件,作为项目的配置文件. 有时候你也会从后台读取到一个json的文件,这节课就学习如何在webpack环境中使用Json. 如果你会we ...

  6. IdentityServer4 密码模式实现

    1.  修改 Config.cs using System.Collections; using System.Collections.Generic; using IdentityServer4.M ...

  7. 解决ASP.NET MVC 下使用SQLite 报no such table的问题

    观察后发现项目中数据库的存放位置不正确. Web项目添加到App_Data文件夹下, 文件始终不复制 Web.Config文件下的连接字符串 <add name="SQLiteconn ...

  8. org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.

    当我用Springboot和mybatis进行延迟加载时候报出如下的错误: org.apache.ibatis.executor.loader.javassist.JavassistProxyFact ...

  9. 【C#小知识】C#中一些易混淆概念总结(八)---------解析接口 分类: C# 2014-02-18 00:09 2336人阅读 评论(4) 收藏

     这一篇主要来解析关于面向对象中最总要的一个概念--接口. 对于接口来说,C#是有规定使用Interface关键字来声明接口.它的声明是和类一致的.可以说接口就是一个特殊的抽象类.如下代码: cl ...

  10. (转)linux内核调优参数对比和解释

    [net] ######################## cat /proc/sys/net/ipv4/tcp_syncookies # 默认值:1 # 作用:是否打开SYN Cookie功能,该 ...