首页 技术 正文
技术 2022年11月10日
0 收藏 889 点赞 4,234 浏览 3040 个字

F                                                                                                                        Find a wayPass 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.  

Input

The 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  

Output

For 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 4Y.#@…..#..@..M4 4Y.#@…..#..@#.M5 5Y..@..#….#…@..M.#…# 

Sample Output

668866

本题主要为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++];
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295