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. Django signal 信号机制的使用

    Django中提供了"信号调度",用于在框架执行操作时解耦,当某些动作发生的时候,系统会根据信号定义的函数执行相应的操作 一.Django中内置的 signal 类型主要包含以下几 ...

  2. (4)Oracle基础--操作表中数据

    · 添加数据 <1> INSERT 语句 ① 向表中所有字段添加值   语法: INSERT INTO table_name (column1,column2...) VALUES(val ...

  3. 数组内数据不使用for循环实现多个移动

    题目: 有序数组中加入一个新的数据,需保持数组有序,如何操作? 方式A :for循环将后续数组依次后移. 方式B :内存拷贝 代码: /******************************** ...

  4. 更换SSL证书

    1.申请证书,需要提供完整域名(例如:xxx.aaa.com),会和证书完全匹配. 2.将证书上传到web服务器,例如我的nginx,在server中指定证书路径. 3.重启web服务器.(这个证书和 ...

  5. Redhead安装VMware Tools

    Redhead安装VMware Tools VMware Tools 是款插件,便于在图形化界面中移动鼠标和复制拖动文件,对于redhead等命令符界面用处不大. 2.挂载光盘安装方法

  6. 【9】JMicro微服务-发布订阅消息服务

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 1. JMicro消息服务目前实现特性 a. JMicro只支持发布订阅消息服务,不支持队列式消息服务: b. 不支持消息持 ...

  7. 【LOJ 2542】【PKUWC2018】 随机游走(最值反演 + 树上期望dp)

    哇我太菜啦555555 不妨钦定我们需要访问的点集为$S$,在$S$已知的情况下,我们令$f(x) $表示从$x$走到点集$S$中任意一点的期望步数. 若$x∈S$,则显然$f(x)=0$,否则$f[ ...

  8. swift 3.0 正则表达式查找/替换字符

    1.什么是正则表达式 正则表达式,又称正规表示法.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式使用单个字符 ...

  9. Java之IO(七)ObjectInputStream和ObjectOutputStream

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7003536.html 1.前言 本章介绍Java字节流中重要的成员,对象流ObjectInputStream和O ...

  10. 手淘适配-flexible

    目标 拿一个双11的Mobile页面来做案例,比如你实现一个类似下图的一个H5页面: 目标很清晰,就是做一个这样的H5页面. 痛点 虽然H5的页面与PC的Web页面相比简单了不少,但让我们头痛的事情是 ...