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 ...
随机推荐
- Spring——自动装配(@Autowired/@Profile/底层组件)
本文介绍Spring中关于自动装配的方法和规则,以及@Profile动态激活的用法和一个例子. 一.@Autowired自动装配 @Autowired注解可以加在构造器.属性.方法.方法参数上. 自动 ...
- 关于responseType的值
http请求有个responseType, 用来设置返回值,默认是'',等同于text,数据格式的转换是浏览器处理的 我们还会用到json,buffer,blob json:是我们经常遇到后端返回的数 ...
- APP图标在线生成
在线生成安卓APP图标生成 图标在线 在线图标 安卓图标 生成图标 https://icon.wuruihong.com/ 在线png图片压缩 png压缩 https://compresspng.c ...
- B树(B-树) 、B+树
B树(B-树) 1.B-树(B树)的基本概念B-树中所有结点中孩子结点个数的最大值成为B-树的阶,通常用m表示,从查找效率考虑,一般要求m>=3.一棵m阶B-树或者是一棵空树,或者是满足以下条件 ...
- 并发编程之线程池ThreadPoolExecutor
前言 在我们平时自己写线程的测试demo时,一般都是用new Thread的方式来创建线程.但是,我们知道创建线程对象,就会在内存中开辟空间,而线程中的任务执行完毕之后,就会销毁. 单个线程的话还好, ...
- codewars--js--Large Factorials--阶乘+大数阶乘
问题描述: In mathematics, the factorial of integer n is written as n!. It is equal to the product of n a ...
- 避免XSS攻击
遭遇XSS攻击怎么解决 XSS的攻击手段 利用JavaScript或DOM方式进行攻击,XSS(脚本注入)提交,然后进行页面展示,影响页面的正常结构,还可以做钓鱼网站,来盗取用户的信息. 比如在页面评 ...
- 表关联使用INNER JOIN实现更新功能
准备一些数据,创建2张表,表1为学生表: CREATE TABLE [dbo].[Student] ( [SNO] INT NOT NULL PRIMARY KEY, ) NOT NULL, ,) N ...
- Learning links
技术文档.API 和代码示例 _ Microsoft Docs _NET 文档 _ Microsoft Docs TutorialsTeacher_C# 菜鸟教程_C# 圣殿骑士<博客园精华集& ...
- PMP--2. 项目启动前准备工作
####################################################### 概述:在第一章节的1.1-1.7中基本都是介绍的<PMBOK>的理论概念以及 ...