2016HUAS暑假集训训练题 D - 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.
Input
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
Sample Input
Sample Output
本题主要为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的更多相关文章
- 2016huas暑假集训训练题 G-Who's in the Middle
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数 刚开始以为是按数学中的 ...
- 2016HUAS暑假集训训练题 G - Oil Deposits
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- 2016HUAS暑假集训训练题 F - 简单计算器
Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运 ...
- 2016HUAS暑假集训训练题 E - Rails
There is a famous railway station in PopPush City. Country there is incredibly hilly. The station wa ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- 2016HUAS暑假集训训练2 O - Can you find it?
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/O 这道题是一道典型二分搜素题,题意是给定3个数组 每个数组的数有m个 再给定l个s ...
- 2016HUAS暑假集训训练2 L - Points on Cycle
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/L 这是一道很有意思的题,就是给定一个以原点为圆心的圆,然后给定 一个点 求最大三 ...
- 2016HUAS暑假集训训练2 K - Hero
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/K 这也是一道贪心题,刚开始写时以为只要对每一敌人的攻击和血的乘积进行从小到大排序即 ...
- 2016HUAS暑假集训训练2 J - 今年暑假不AC
题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/J 此题要求是计算能够看到最多的节目 ,贪心算法即可,首先对结束时间排序,然后在把开 ...
随机推荐
- hadoop常用命令
hdfs fsck / 副本数量 hdfs dfsadmin -report hdfs大小
- 【转载】C++中public,protected,private访问
第一:private, public, protected 访问标号的访问范围. 假如我们约定: 类内部-----指的是当前类类型的定义中,以及其成员函数的声明和定义中: 类外部-----指的是不在当 ...
- hdu 5692 Snacks 线段树+dfs
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- MySQL导入sql脚本 导出数据库
导出数据库 不能停止服务 cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) mysqldump -u用户名 -p 数据库名 > 导出的文件名 ...
- C#根据html生成PDF
使用iTextSharp 控件 iTextSharp包括几个dll. 主要iTextSharp版本包含:——itextsharp.dll:核心库——itextsharp.xtra.dll:额外的功能( ...
- java出错
某天cmd运行java突然莫名其妙的出错:(之前还好好的) Error occurred during initialization of VMjava/lang/NoClassDefFoundErr ...
- 门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统
门店 车销 批发送货 商超 快销专用扫描打印开单手持PDA移动销售管理系统的详细介绍 一. 以PDA等移动终端为媒介,随时随地掌握门店信息. 二. 后台集成了数据统计.多指标分析.销售.库存.会员管理 ...
- const int *
5.Please choose the right statement about constusage: A.const int a;//const interger B.int const a;/ ...
- three.js 显示一条线
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【BZOJ1060】[ZJOI2007]时态同步 树形DP
[BZOJ1060][ZJOI2007]时态同步 Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3-.进行标号.电路 ...