为了能到远方,脚下的每一步都不能少.|

fishcanfly

园龄:8年10个月粉丝:0关注:4

【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;
        }
    }
复制代码

 

本文作者:love

本文链接:https://www.cnblogs.com/fishcanfly/p/16861453.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   fishcanfly  阅读(52)  评论(1编辑  收藏  举报
//雪花飘落效果
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起