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. POJ 3274 HASH

    题目链接:http://poj.org/problem?id=3274 题意+思路: 点击这里 补充:因为有减法运算,所以可能会造成运算后结果为负数,所以需要把结果统一转换成正数[不然数组下标访问不到 ...

  2. json时间格式化问题

    function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.rep ...

  3. Codeforces Round #333 (Div. 2)

    水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...

  4. Robotium ant 报错Unable to find instrumentation info for: ComponentInfo{project/android.test.InstrumentationTestRunner}

    [echo] Running tests ... [exec] INSTRUMENTATION_STATUS: id=ActivityManagerService [exec] INSTRUMENTA ...

  5. (转)STORM启动与部署TOPOLOGY

    STORM启动与部署TOPOLOGY 启动ZOOPKEEPER zkServer.sh start 启动NIMBUS storm nimbus & 启动SUPERVISOR storm sup ...

  6. Hadoop学习笔记(1)

    Doug Cutting Lucene(索引引擎)---Nutch(搜索Data抓取)---Hadoop 1997:Lucene 2003:GFS 2004:NDFS\MapReduce\Nutch ...

  7. HDU 1754区间最值 & SPLAY

    真是亲切的1754啊..第一道傻逼版的线段树做的是这个,后来学了zkw做的是这个,在后来决定打lrj线段树又打了一遍,如今再用splay和老朋友见面   从上到下依次为:加了读入优化的splay,sp ...

  8. hdu2612 Find a way

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

  9. OpenResty 简单编写一个Module

    使用 Lua module 来进行 Lua 代码的复用是推荐的做法.然后在用户代码中直接用require()来调用 module代码: local myTest = {} function myTes ...

  10. Linux远程传输命令之scp使用方法

      首先用pwd命令确定文件全路径   1.获取远程服务器上的文件 cykdeMacBook-Pro:~ cyk$ scp cyk@10.211.55.5:/home/cyk/Desktop/hi.t ...