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. 

InputThe 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 
OutputFor 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 题意:求2个点到同一个点的最短路程 注意:某些点可能两个点不能同时到达,所以要用两个数组分别记录两次广搜的路径到达情况 还是强调多个输入,每次都要数组初始化、队列清空 代码:
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
class Node{
int x;
int y;
int step;
public Node(int x,int y,int step){
this.x=x; this.y=y; this.step=step;
}
}
public class Main{
static final int N=205;
static int n,m;
static int dx[]={0,0,1,-1};
static int dy[]={1,-1,0,0};
static char map[][]=new char[N][N];
static int s1[][]=new int[N][N];
static int s2[][]=new int[N][N];
static boolean vis[][]=new boolean[N][N];
static ArrayDeque<Node> q=new ArrayDeque<Node>();
static void init(){
for(int i=0;i<N;i++) Arrays.fill(s1[i], 0);
for(int i=0;i<N;i++) Arrays.fill(s2[i], 0);
}
static void bfs(int sx,int sy,int s[][]){
while(!q.isEmpty()) q.poll();
vis[sx][sy]=true;
q.offer(new Node(sx,sy,0));
while(!q.isEmpty()){
Node t=q.poll();
for(int i=0;i<4;i++){
int xx=t.x+dx[i];
int yy=t.y+dy[i];
if(xx<0||yy<0||xx>=n||yy>=m ||vis[xx][yy]||map[xx][yy]=='#') continue;
vis[xx][yy]=true;
s[xx][yy]=t.step+1;
q.offer(new Node(xx,yy,t.step+1));
} }
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
n=scan.nextInt();
m=scan.nextInt();
for(int i=0;i<n;i++) map[i]=scan.next().toCharArray(); int yx=0,yy=0,mx=0,my=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='Y'){
yx=i; yy=j;
}
else if(map[i][j]=='M'){
mx=i; my=j;
} init();
for(int i=0;i<N;i++) Arrays.fill(vis[i], false);
bfs(yx,yy,s1);
for(int i=0;i<N;i++) Arrays.fill(vis[i], false);
bfs(mx,my,s2); int min=2147483647;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='@' && s1[i][j]!=0 &&s2[i][j]!=0)
min=Math.min(min, s1[i][j]+s2[i][j]);
System.out.println(min*11);
}
}
}

HDU2612 Find a way (双广搜)的更多相关文章

  1. nyoj 999——师傅又被妖怪抓走了——————【双广搜】

    师傅又被妖怪抓走了 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...

  2. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  3. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  4. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  5. hdu.1043.Eight (打表 || 双广 + 奇偶逆序)

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  6. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  7. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  8. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  9. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

随机推荐

  1. Go语言基础之接口(面向对象编程下)

    1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...

  2. 字符串转数字 (With C++)

    1.stoi().stof().stod() 实现字符串转 int.float.double. stoi -> string to integer stof -> string to fl ...

  3. Shiro -- (二) 身份验证基本流程

    简介: 在 shiro 中,用户需要提供 principals (身份)和 credentials(证明)给 shiro,从而应用能验证用户身份: principals:身份,即主体的标识属性,可以是 ...

  4. Redis入门(介绍、搭建)——Windows、Centos环境

    一.介绍 Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cac ...

  5. 获取Data和Log默认路径

    使用SERVERPROPERTY()来得到Data和Log的默认路径: InstanceDefaultDataPath和InstanceDefaultLogPath分别返回默认数据和日志目录. DEC ...

  6. STM32固件库和自定义工程模板

    固件库结构 本文使用的固件库是STM32F10x_StdPeriph_Lib_V3.5.0,可以在官网获取.该固件库包含四个文件夹和一个库的说明文档,如下图所示,stm32f10x_stdperiph ...

  7. 吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Spring_autowire

    <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www. ...

  8. 【python基础语法】第8天作业练习题

    """ # 第一题: # 要求:请将数据读取出来,转换为以下格式 {'data0': '数据aaa', 'data1': '数据bbb', 'data2': '数据ccc ...

  9. shell脚本监测进程并重启

    本人实例: #!/bin/bash ps -ef | grep elastic | grep -v grepif [ $? -ne 0 ]thenecho "start process... ...

  10. GCD相关

    板子: ? gcd(b, a % b) : a; } POJ1930 题意:给你一个无限循环小数,给到小数点后 9 位,要求这个数的分数形式. 解法: 要想解决这道题,首先应该了解如何将循环小数化为分 ...