【atcoder abc276 】(a* 搜索)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*; /**
*
* @author fishcanfly
*/
public class Main {
/**
* main入口由OJ平台调用
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int h, w;
String[] words = br.readLine().split("\\s+");
h = Integer.valueOf(words[0]);
w = Integer.valueOf(words[1]);
char[][] board = new char[h][w];
int[] source = new int[]{0, 0};
for (int i = 0; i < h; i++) {
board[i] = br.readLine().toCharArray();
for (int j = 0; j < w; j++) {
if (board[i][j] == 'S') {
source = new int[]{i, j};
}
}
}
br.close();
List<int[]> list = new ArrayList<>();
int[][] dx = new int[][]{
{0, 1}, {0, -1}, {1, 0}, {-1, 0}
};
for (int i = 0; i < 4; i++) {
int newx = source[0] + dx[i][0];
int newy = source[1] + dx[i][1]; if (newx >= 0 && newx < h && newy >= 0 && newy < w && board[newx][newy] == '.') {
list.add(new int[]{newx, newy});
}
} for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
// boolean[][] visited = new boolean[h][w];
int[] a1 = list.get(i);
int[] b1 = list.get(j);
if (travel(a1, b1, h, w, board)) {
System.out.println("Yes");
return;
}
}
} System.out.println("No");
} public static boolean travel(int[] source, int[] target, int h, int w, char[][] board) {
int max = 0;
boolean[][] visited = new boolean[h][w];
int x = source[0];
int y = source[1];
visited[x][y] = true;
PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
int distance1 = Math.abs(o1[0] - target[0]) + Math.abs(o1[1] - target[1]);
int distance2 = Math.abs(o2[0] - target[0]) + Math.abs(o2[1] - target[1]);
return o2[2] + distance2 - distance1 - o1[2];
}
});
queue.add(new int[]{x, y, 0}); while (!queue.isEmpty()) {
int[] u = queue.poll();
x = u[0];
y = u[1];
int d = u[2]; if (x == target[0] && y == target[1]) {
max = Math.max(max, d);
} int[][] dx = new int[][]{
{0, 1}, {0, -1}, {1, 0}, {-1, 0}
};
for (int i = 0; i < 4; i++) {
int newx = x + dx[i][0];
int newy = y + dx[i][1]; if (newx >= 0 && newx < h && newy >= 0 && newy < w && board[newx][newy] == '.' && !visited[newx][newy]) {
visited[newx][newy] = true;
queue.add(new int[]{newx, newy, d + 1});
}
} } return max >= 2;
}
}
【atcoder abc276 】(a* 搜索)的更多相关文章
- Atcoder Grand Contest 020 E - Encoding Subsets(记忆化搜索+复杂度分析)
Atcoder 题面传送门 & 洛谷题面传送门 首先先考虑如果没有什么子集的限制怎样计算方案数.明显就是一个区间 \(dp\),这个恰好一年前就做过类似的题目了.我们设 \(f_{l,r}\) ...
- 2018.09.18 atcoder Many Formulas(搜索)
传送门 感觉自己搜索能力退化了,这种弱智搜索写了整整5min,这样下去比赛会凉的. 看来得多练练题了. 代码: #include<bits/stdc++.h> #define ll lon ...
- Atcoder F - Mirrored(思维+搜索)
题目链接:http://arc075.contest.atcoder.jp/tasks/arc075_d 题意:求rev(N)=N+D的个数,rev表示取反.例如rev(123)=321 题解:具体看 ...
- 【Atcoder】AGC022 C - Remainder Game 搜索
[题目]C - Remainder Game [题意]给定n个数字的序列A,每次可以选择一个数字k并选择一些数字对k取模,花费2^k的代价.要求最终变成序列B,求最小代价或无解.n<=50,0& ...
- AtCoder Grand Contest 012 B Splatter Painting(记忆化搜索)
题意: 给一个包含N个顶点,M条边,无自环和重边的简单无向图,初始每个点颜色都为0,每条边的长度为1,连接着ai,bi两个节点.经过若干个操作, 每次将与某个点vi距离不超过di的所有点染成某种颜色c ...
- AtCoder Regular Contest 090
C - Candies 链接:https://arc090.contest.atcoder.jp/tasks/arc090_a 题意:从左上角走到右下角,只能向右和向下走,问能最多能拿多少糖果. 思路 ...
- Atcoder Beginner Contest 070 D - Transit Tree Path
题意:n个点,n-1条边,组成一个无向的联通图,然后给出q和k,q次询问,每次给出两个点,问这两个点之间的最短距离但必须经过k点. 思路:我当时是用优化的Dijkstra写的(当天刚学的),求出k点到 ...
- 【AtCoder】CODE FESTIVAL 2017 Final
A - AKIBA 模拟即可 代码 #include <bits/stdc++.h> #define fi first #define se second #define pii pair ...
- 记第一场atcoder和codeforces 2018-2019 ICPC, NEERC, Northern Eurasia Finals Online Mirror
下午连着两场比赛,爽. 首先是codeforses,我和一位dalao一起打的,结果考炸了,幸亏不计rating.. A Alice the Fan 这个就是记忆化搜索一下预处理,然后直接回答询问好了 ...
- ATCODER ABC 099
ATCODER ABC 099 记录一下自己第一场AK的比赛吧...虽然还是被各种踩... 只能说ABC确实是比较容易. A 题目大意 给你一个数(1~1999),让你判断它是不是大于999. Sol ...
随机推荐
- 一文搞懂 Vue3 defineModel 双向绑定:告别繁琐代码!
前言 随着vue3.4版本的发布,defineModel也正式转正了.它可以简化父子组件之间的双向绑定,是目前官方推荐的双向绑定实现方式. vue3.4以前如何实现双向绑定 大家应该都知道v-mode ...
- vue-element-admin iframes 组件 保留 iframe 操作状态
由于没有时间去维护这个功能,这个仓库我暂停了,当前博客内容和代码只作为实现思路参考 代码贴前面,gitee地址:https://gitee.com/chkhk/vue-element-admin 可以 ...
- 普及模拟2 +【LGR-155-Div.3】洛谷基础赛 #3 &「NnOI」Round 2
普及模拟2 \(T1\) 地址 \(0pts\) 简化题意:判断一个 \(IP\) 地址是否合法(数据保证字符串中存在且仅存在4个被字符分开的整数),若不合法则将其改正. 部分分: \(0pts\) ...
- 编写高效的代码,你应该了解Array、Memory、ReadOnlySequence
针对"缓冲区"编程是一个非常注重"性能"的地方,我们应该尽可能地避免武断地创建字节数组来存储读取的内容,这样不但会导致大量的字节拷贝,临时创建的字节数组还会带来 ...
- 2023牛客暑期多校训练营6 ABCEG
比赛链接 A 题解 方法一 知识点:并查集,树形dp,背包dp. 因为需要路径中的最大值,因此考虑按边权从小到大加入图中,保证通过这条边产生贡献的点对已经全部出现. 在加边的同时进行树上背包,答案存在 ...
- 文心一言 VS 讯飞星火 VS chatgpt (199)-- 算法导论15.2 1题
一.用go语言,对矩阵规模序列(5,10,3,12,5,50,6),求矩阵链最优括号化方案. 文心一言,代码正常运行: 在Go语言中,为了找到矩阵链乘法的最优括号化方案,我们通常会使用动态规划(Dyn ...
- 将JavaBean对象转换为Map集合
使用jackson-databind可以将JavaBean对象属性转换为Map集合. 添加配置依赖: <dependency> <groupId>com.fasterxml.j ...
- Qt三方库开发技术:QXlsx介绍、编译和使用
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- 图片验证码pillow模块
安装下载 pip install pillow 使用 需要引入PIL里面的Image from PIL import Image # mode为采用什么色系,size为大小px,color为颜色 im ...
- day01---操作系统安装环境准备
虚拟机安装操作系统步骤 1.新建虚拟主机 2.选择自定义 3.稍后安装操作系统 4.操作系统选择linux 5.选择存放位置 6.cpu和核数选择,默认即可 7.内存分配 8.网络选择 9.控制器类型 ...