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;
        }
    }

 

原文地址:http://www.cnblogs.com/fishcanfly/p/16861453.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性