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 ...
 
随机推荐
- lua学习之深入函数第二篇
			
深入函数 2 非全局的函数 函数是第一类值,函数可以存储到全局变量,局部变量,table 字段中 lua 函数库中的大部分函数存储到 table 字段中 Lib = {} Lib.foo = func ...
 - ExecutionContext(执行上下文)综述
			
>>返回<C# 并发编程> 1. 简介 2. 同步异步对比 3. 上下文的捕获和恢复 4. Flowing ExecutionContext vs Using Synchron ...
 - 【57】目标检测之Anchor Boxes
			
Anchor Boxes 到目前为止,对象检测中存在的一个问题是每个格子只能检测出一个对象,如果你想让一个格子检测出多个对象,你可以这么做,就是使用anchor box这个概念. 我们还是先吃一颗栗子 ...
 - springboot~HttpPut开启application/x-www-form-urlencoded
			
在使用spring框架时,默认情况下@RequestParam注解只到接受Get和Post请求参数 ,而对于Put来说默认是使用@ReqeustBody注解的,如果希望为Put也开启@RequestP ...
 - 这 100 道 Python 题,拿去刷!!!
			
2020年,学 Python 还有价值吗? 根据 2020 年 2 月的 TIOBE 编程语言排行榜显示,Python仍然稳居第三位. 此排行榜排名基于互联网上有经验的程序员. 课程和第三方厂商的数量 ...
 - SSH-Secure-Shell 3.2.9 build283版本,创建直接打开文件传输的快捷方式的方法
			
2019-12-31 16:21:23 版本信息: 在安装目录下新建快捷方式 目标填写:"D:\SSH-Secure-Shell\SshClient.exe" /f 图标选择,系统 ...
 - Vue与原生APP的相互交互
			
现在好多APP都采用了Hybrid的开发模式,这种模式特别适合那些内容变动更新较大的APP,从而使得开发和日常维护过程变得集中式.更简短.更经济高效,不需要纯原生频繁发布.但有利肯定有弊咯,性能方面能 ...
 - git需要设置再次弹出输入账号密码
			
今天在用命令行pull线上代码到本地时遇到一个尴尬的问题,因为新下载的git貌似默认了在pull,push代码时只弹出一次输入账号密码,反正我这里是这样的. 开始在pull线上代码的时候不小心密码输错 ...
 - C#效率优化(4)-- 编译器对数组遍历的优化
			
在平时开发过程中,数组是我们使用频率最高的类型之一,在使用定长列表时,数组可以说是最佳方案,这也是我们最熟悉的数据结构之一. 在C#中使用数组,可以获取在内存上连续的相同类型的一组变量,在连续访问时可 ...
 - C# WPF发票打印
			
微信公众号:Dotnet9,网站:Dotnet9.问题或建议,请网站留言: 如果您觉得Dotnet9对您有帮助,欢迎赞赏 C# WPF发票打印 内容目录 实现效果 业务场景 编码实现 本文参考 源码下 ...