HDU2612 Find a way (双广搜)
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 (双广搜)的更多相关文章
- nyoj 999——师傅又被妖怪抓走了——————【双广搜】
师傅又被妖怪抓走了 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟 ...
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- hdu.1043.Eight (打表 || 双广 + 奇偶逆序)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
随机推荐
- Hibernate框架预览以及基础介绍
前言 从本节我们开始进入到对于Hibernate框架的学习,当前Hibernate框架还未正式发布6.0稳定版本,所以这里我们以5.4.12Final版本进行讲解. Hibernate框架 Hiber ...
- Django 系列
Django基础 Django框架简介 Django 静态文件 Django request对象与ORM简介 Django路由系统 Django之视图层 Django之模板层 Django的setti ...
- JDK SPI 机制
一.概述 最早看到 SPI 这个机制是在 dubbo 实现 中,最近发现原来也不是什么新东西,竟然就是 JDK 中内置的玩意,今天就来一探究竟,看看它到底是什么玩意! SPI的全称是 Service ...
- 范式通俗理解:1NF、2NF、3NF和BNCF
https://blog.csdn.net/wyh7280/article/details/83350722 范式通俗理解:1NF.2NF.3NF和BNCF原创hongiii 最后发布于2018-10 ...
- MySql学习-1.MySql的安装:
1.安装包的下载(mysql-v5.7.25 )(NavicatforMySQL_11.2.15): 链接:https://pan.baidu.com/s/166hyyYd3DMjYhMwdW805F ...
- Eclipse+ADT+Android SDK搭建安卓开发环境
第一步:打开[Android.rar]压缩包,如图所示[评论区回复我,压缩包地址] 第二步:配置环境变量 (1) 解压[android-sdk_r24.4.1-windows.zip]压缩包 (2) ...
- vue-cli-service 报错
错误内容: vue-cli-service serve /bin/sh: vue-cli-service: command not found error Command failed with ex ...
- Notability
Notability 上课记笔记.听网课→Notability 有录音功能, 在原来笔记中新添加空白行(选中之后下移) Notability常用的功能总结 1.纸张有颜色2.荧光笔会盖住文字3.套索工 ...
- lint-staged 使用教程
lint-staged 是一个在git暂存文件上运行linters的工具,当然如果你觉得每次修改一个文件就给所有文件执行一次lint检查不恶心的话,这个工具对你来说就没有什么意义了,请直接关闭即可. ...
- [USACO19DEC]Tree Depth P
题意 求逆序对为\(k\)的\(n\)排列中,生成的笛卡尔数,每个位置的深度和.\(n\le 300\) 做法 设\(f_{k}\)为\(n\)排列中逆序对为\(k\)的个数,其生成函数为:\[\pr ...