|
| 1 | +```java |
| 2 | +import java.awt.*; |
| 3 | +import java.io.*; |
| 4 | +import java.util.ArrayDeque; |
| 5 | +import java.util.Queue; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class BJ_19606_Escape_Room { |
| 9 | + |
| 10 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + private static StringTokenizer st; |
| 13 | + |
| 14 | + private static int M, N; |
| 15 | + private static int[][] matrix; |
| 16 | + private static boolean[] checked; |
| 17 | + private static boolean[][] visited; |
| 18 | + private static Queue<Point> q; |
| 19 | + |
| 20 | + public static void main(String[] args) throws IOException { |
| 21 | + init(); |
| 22 | + sol(); |
| 23 | + } |
| 24 | + |
| 25 | + private static void init() throws IOException { |
| 26 | + M = Integer.parseInt(br.readLine()); |
| 27 | + N = Integer.parseInt(br.readLine()); |
| 28 | + |
| 29 | + checked = new boolean[1_000_001]; |
| 30 | + matrix = new int[M + 1][N + 1]; |
| 31 | + for (int i = 1; i <= M; i++) { |
| 32 | + st = new StringTokenizer(br.readLine()); |
| 33 | + for (int j = 1; j <= N; j++) { |
| 34 | + matrix[i][j] = Integer.parseInt(st.nextToken()); |
| 35 | + } |
| 36 | + } |
| 37 | + visited = new boolean[M + 1][N + 1]; |
| 38 | + q = new ArrayDeque<>(); |
| 39 | + } |
| 40 | + |
| 41 | + private static void sol() throws IOException { |
| 42 | + bfs(); |
| 43 | + bw.flush(); |
| 44 | + bw.close(); |
| 45 | + br.close(); |
| 46 | + } |
| 47 | + |
| 48 | + private static void bfs() throws IOException { |
| 49 | + q.offer(new Point(1, 1)); |
| 50 | + visited[1][1] = true; |
| 51 | + |
| 52 | + while (!q.isEmpty()) { |
| 53 | + Point cur = q.poll(); |
| 54 | + |
| 55 | + if (cur.x == M && cur.y == N) { |
| 56 | + bw.write("yes"); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + int target = matrix[cur.x][cur.y]; |
| 61 | + if (checked[target]) continue; |
| 62 | + checked[target] = true; |
| 63 | + |
| 64 | + for (int i = 1; i * i <= target; i++) { |
| 65 | + if (target % i != 0) continue; |
| 66 | + |
| 67 | + int nx = i; |
| 68 | + int ny = target / i; |
| 69 | + |
| 70 | + if (!OOB(nx, ny) && !visited[nx][ny]) { |
| 71 | + q.offer(new Point(nx, ny)); |
| 72 | + visited[nx][ny] = true; |
| 73 | + } |
| 74 | + if (!OOB(ny, nx) && !visited[ny][nx]) { |
| 75 | + q.offer(new Point(ny, nx)); |
| 76 | + visited[ny][nx] = true; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + bw.write("no"); |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean OOB(int x, int y) { |
| 84 | + return x < 1 || x > M || y < 1 || y > N; |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | +``` |
0 commit comments