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