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. js string 和 json 互转

    var o = JSON.parse('{"a": 8}'); JSON. stringify(o);

  2. python http post json

    直接上代码吧 #coding=utf-8 import os import urllib import urllib2 import re import cookielib import json h ...

  3. 【DC010沙龙年度合集】顶尖Hacking技术盛宴(文末福利)

    岁末盘点,让我们一起回顾2017年DEFCON GROUP 010带来的那些最前端的Hacker技术,体验原汁原味的mini DEFCON黑客大会,满满的干货帮你开启Hacker技术大门     &g ...

  4. Kafka数据可靠性与一致性解析

    Partition Recovery机制 每个Partition会在磁盘记录一个RecoveryPoint, 记录已经flush到磁盘的最大offset.broker fail 重启时,会进行load ...

  5. javaweb 项目的异常处理

    首先关于异常的分类: java 中关于异常的分类情况是:throwable 是所有异常和错误的基类,下面在分为Error 和 Exception: 简单的异常体系结构如下图所示: 其中Exceptio ...

  6. Pycharm使用Git

    Pycharm使用Git 1.设置git程序路径 2.设置github连接 3.创建git respository 4.提交文件 5.共享给GitHub 6.修改文件push到版本库 7.从版本库ch ...

  7. docker微服务部署之:七、Rancher进行微服务扩容和缩容

    docker微服务部署之:六.Rancher管理部署微服务 Rancher有两个特色用起来很方便,那就是扩容和缩容. 一.扩容前的准备工作 为了能直观的查看效果,需要修改下demo_article项目 ...

  8. 【LeetCode】390. 消除游戏

    题目 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直 ...

  9. 3、Xamarin Forms 调整安卓TabbedPage 下置

    降低学习成本是每个.NET传教士义务与责任. 建立生态,保护生态,见者有份.   教程晦涩难懂是我的错误. 对于默认的TabbedPage 上面进行页面切换 上面是安卓默认的情况 对我们大部分人来说都 ...

  10. 剑指offer四十之数组中只出现一次的数字

    一.题目 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 二.思路 建一个hashMap,统计各数字出现的次数,然后遍历hashMap,输出出现一次的数字 ...