首页 > 其他分享 >【atcoder abc276 】(a* 搜索)

【atcoder abc276 】(a* 搜索)

时间:2022-11-05 22:00:21浏览次数:63  
标签:atcoder newy int source newx abc276 搜索 && new

    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,newy,int,source,newx,abc276,搜索,&&,new
From: https://www.cnblogs.com/fishcanfly/p/16861453.html

相关文章

  • 关闭谷歌历史搜索下拉框
    ​​https://chrome.google.com/webstore/detail/hcos-hide-chrome-omnibox/aldijnffnfojelcpcfoekkeifffkhldo/related?hl=en&authuser=0​​作者牛B 怎么关闭chrome地址......
  • Atcoder Beginner Contest ABC 275 Ex (H) Monster 题解
    先明确\(a_i\)是一个怪物的血量,\(b_i\)是攻击的代价。发现如果我们想攻击一个怪物,不如找出一个极大的包含它的区间,满足这个区间内所有怪物的攻击代价都不大于它本身的代价,......
  • AtCoder Regular Contest 068
    C简单,D有点意思,但没啥用,不写。F有点好玩,想了一个小时,看了一上午题解,弄明白了。E-SnukeLine\(n\)个物品,第\(i\)个物品在\([l_i,r_i]\)间。你初始在第0格,......
  • Sugoroku 4 (Atcoder abc275 T5) DP
    题目描述题目链接https://atcoder.jp/contests/abc275/tasks/abc275_e题意从\(0\)到\(n\)有\(n+1\)个方格,你现在在第\(0\)个格子。每次移动可以随机走\(1\)......
  • 数据结构 玩转数据结构 6-5 二分搜索树的查询操作
    0课程地址https://coding.imooc.com/lesson/207.html#mid=13458 1重点关注1.1二分搜索树查询代码实现见3.1 2课程内容 3......
  • 关闭 Win10 搜索框的网络搜索功能
    1概述Win10的搜索框也挺好用的,但美中不足的是搜索的时候会出现网络搜索,且还会显示热门搜索,对于只用这个功能来搜索本机文件的来说,网络搜索功能就没必要存在了。本文记录......
  • Atcoder Beginner Contest 271(A~G)
    省流:赛时F假了。赛时A转进制;B简单vector。C简单贪心模拟,大概就是能走就走,不能走就先不走(较大)或者存起来(较小),最后走存起来的。挂了3发,自闭了。D第一眼看成了......
  • 代码随想录day41 | 343. 整数拆分 96. 不同的二叉搜索树
    343.整数拆分题目|文章思路一个动态规划问题最重要的就是找到递推公式,找到递推公式,整个题就已经解出来了。这道题看到时会想分成几份比较好,等于10时与之前的数有什么......
  • [Java web]-- jquery自动填充input框(如百度搜索一样,出现模糊提示)
    一、基本思路如下 第一个html页面:<htmllang="en"><head>  <metacharset="GBK"/>  <title>页面</title>  <linkrel="stylesheet"href="http://code.jq......
  • 98.验证二叉搜索树
    给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。有效 二叉搜索树定义如下:节点的左子树只包含 小于 当前节点的数。节点的右子树只包含 大于 ......