F                                                                                                                        Find a way
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
本题主要为bfs双向查找,用bfs查找也行,记录两个人到@的时间  最后再求最短时间   但用c++容易超时,用java只用了800多ms 
附上AC代码:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Scanner;
public class Main {
static int queue[] = new int[40005];
static int queue1[] = new int[40005];
static int top;
static int under;
static int top1;
static int under1;
static int dir[][] = { { -1, 0 }, { 1, 0 }, { 0, 1 }, { 0, -1 } };
static String a[] = new String[205];
public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
Scanner s = new Scanner(new BufferedInputStream(System.in));
int x2;
int y2;
int px, py, x, start = 0, en = 0, y, v, v2;
int px1;
int py1; while (s.hasNext()) {
x = s.nextInt();
y = s.nextInt();
int visit1[][] = new int[x][y];
int visit[][] = new int[x][y];
for (int i = 0; i < x; i++) { //输入地图
a[i] = s.next();
for (int j = 0; j < y; j++) { if (a[i].charAt(j) == 'Y') //Y的位置
start = i * y + j;
if (a[i].charAt(j) == 'M') //M的位置
en = i * y + j;
}
}
//对Y进行bfs搜索 记录的到各点的距离
top = -1;
under = 0;
for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) {
visit[i][j] = 0;
}
}
visit[start / y][start % y] = 1;
queue_push(start);
int x1 = start / y, y1 = start % y; //数组的下标
while (under <= top) {
v = queue_pop();
x1 = v / y;
y1 = v % y;
for (int i = 0; i < 4; i++) {
px = x1 + dir[i][0];
py = y1 + dir[i][1];
if (px >= 0 && px < x && py < y && py >= 0 && visit[px][py] == 0 && a[px].charAt(py) != '#') { //找到符合条件的点
queue_push(px * y + py);
visit[px][py] = visit[x1][y1] + 1; //令 visit为前一个加一 表示Y到此点的距离
}
}
}
//对M进行bfs搜索 具体和Y搜索一致 不再重复
top1 = -1;
under1 = 0;
for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) {
visit1[i][j] = 0;
}
}
visit1[en / y][en % y] = 1;
queue1_push(en);
x2 = en / y;
y2 = en % y;
while (under1 <= top1) {
v2 = queue1_pop();
x2 = v2 / y;
y2 = v2 % y;
for (int i = 0; i < 4; i++) {
px1 = x2 + dir[i][0];
py1 = y2 + dir[i][1];
if (px1 >= 0 && px1 < x && py1 < y && py1 >= 0 && visit1[px1][py1] == 0
&& a[px1].charAt(py1) != '#') {
queue1_push(px1 * y + py1);
visit1[px1][py1] = visit1[x2][y2] + 1;
}
}
}
int min = 100000;
for (int i = 0; i < x; i++) { //对Y,M到所有的的@的距离进行筛选 找到最短路径
for (int j = 0; j < y; j++) {
if (a[i].charAt(j) == '@' && visit[i][j] > 0 && visit1[i][j] > 0) {
min = min > (visit[i][j] - 1 + visit1[i][j] - 1) ? (visit[i][j] - 1 + visit1[i][j] - 1) : min;
}
}
}
System.out.println(min * 11);
}
s.close();
}
static void queue_push(int x) {
queue[++top] = x;
}
static void queue1_push(int x) {
queue1[++top1] = x;
}
static int queue_pop() {
return queue[under++];
}
static int queue1_pop() {
return queue1[under1++];
}
}

  

2016HUAS暑假集训训练题 D - Find a way的更多相关文章

  1. 2016huas暑假集训训练题 G-Who's in the Middle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数  刚开始以为是按数学中的 ...

  2. 2016HUAS暑假集训训练题 G - Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  3. 2016HUAS暑假集训训练题 F - 简单计算器

    Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.    Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运 ...

  4. 2016HUAS暑假集训训练题 E - Rails

    There is a famous railway station in PopPush City. Country there is incredibly hilly. The station wa ...

  5. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  6. 2016HUAS暑假集训训练2 O - Can you find it?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/O 这道题是一道典型二分搜素题,题意是给定3个数组 每个数组的数有m个 再给定l个s ...

  7. 2016HUAS暑假集训训练2 L - Points on Cycle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/L 这是一道很有意思的题,就是给定一个以原点为圆心的圆,然后给定 一个点  求最大三 ...

  8. 2016HUAS暑假集训训练2 K - Hero

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/K 这也是一道贪心题,刚开始写时以为只要对每一敌人的攻击和血的乘积进行从小到大排序即 ...

  9. 2016HUAS暑假集训训练2 J - 今年暑假不AC

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/J 此题要求是计算能够看到最多的节目 ,贪心算法即可,首先对结束时间排序,然后在把开 ...

随机推荐

  1. 【工具】Git

    1.安装好Git以后,在开始菜单里找到Git->Git Bash,弹出一个命令窗口 2.设置邮箱 . 3.创建文件夹 4.创建版本库 5.将文件添加到缓存区中去 6.提交文件 7.检查是否还有文 ...

  2. AngularJS学习之HTML DOM

    1.AngularJS为HTML DOM元素的属性提供了绑定应用数据的指令: 2.ng-disabled指令:直接绑定应用程序数据到HTML的disable属性: <div ng-app=&qu ...

  3. matlab坐标外围背景变白色

    set(gcf,'Color',[1,1,1]) 默认图片是这样的: 出图之前使用命令,外围变白后效果如下:

  4. 浩瀚PDA无线POS盘点机(安装盘点程序):盘点结果实时上传到PC电脑端

    手持终端机的盘点部分改进, 1可以通过wifi联网到后台, 2也可以暂存在手持终端机上,盘点完后一次性上传到电脑上.

  5. AngularJS $http

    $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据.在服务器上读取数据: <div ng-app="myApp" ng-controller=&q ...

  6. VS链接过程中与MSVCRT.lib冲突

    vs代码生成有/MT,/MTd,/Md,/MDd四个编译选项,分别代表多线程.多线程调试.多线程DLL.多线程调试DLL. 编译时引用的lib分别为libcmt.li.libcmtd.lib.msvc ...

  7. 使用Maven构建Android项目

    http://www.ikoding.com/build-android-project-with-maven/ 之前一直在做WEB前端项目,前段时间接手第一个Android项目,拿到代码之后,先试着 ...

  8. 每天一个linux命令--locate

    linux下,不知道自己安装的程序放在哪里了,可以使用locate命令进行查找. [hongye@dev107 ~]$ locate activemq.xml /home/hongye/hongyeC ...

  9. apache activemq 学习笔记

    0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...

  10. A hard puzzle

    A hard puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...