|
| 1 | +package BFS.P1938; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + final static int col = 0, row = 1; |
| 9 | + static int N; |
| 10 | + static char[][] map; |
| 11 | + static Position start, dest; |
| 12 | + |
| 13 | + public static void main(String[] args) throws Exception { |
| 14 | + System.setIn(new FileInputStream("src/BFS/P1938/input.txt")); |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + |
| 17 | + N = Integer.parseInt(br.readLine()); |
| 18 | + |
| 19 | + map = new char[N+2][N+2]; |
| 20 | + for (int i = 0; i < N+2; i++) Arrays.fill(map[i], '1'); |
| 21 | + for (int i = 1; i <= N; i++) { |
| 22 | + String line = br.readLine(); |
| 23 | + for (int j = 1; j <= N; j++) { |
| 24 | + map[i][j] = line.charAt(j-1); |
| 25 | + if (map[i][j] == 'B') start = setPosition(start, i, j); |
| 26 | + else if (map[i][j] == 'E') dest = setPosition(dest, i, j); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + System.out.println(bfs()); |
| 31 | + } |
| 32 | + |
| 33 | + static int bfs() { |
| 34 | + Queue<Position> q = new LinkedList<>(); |
| 35 | + boolean[][][] visited = new boolean[N+2][N+2][2]; |
| 36 | + int cnt = 0; |
| 37 | + |
| 38 | + q.offer(start); |
| 39 | + visited[start.i][start.j][start.d] = true; |
| 40 | + while (!q.isEmpty()) { |
| 41 | + int curSize = q.size(); |
| 42 | + while (curSize-- > 0) { |
| 43 | + Position cur = q.poll(); |
| 44 | + if (cur.equals(dest)) return cnt; |
| 45 | + |
| 46 | + Position to; |
| 47 | + to = moveUp(cur); |
| 48 | + if (to != null && !visited[to.i][to.j][to.d]) { |
| 49 | + visited[to.i][to.j][to.d] = true; |
| 50 | + q.offer(to); |
| 51 | + } |
| 52 | + to = moveDown(cur); |
| 53 | + if (to != null && !visited[to.i][to.j][to.d]) { |
| 54 | + visited[to.i][to.j][to.d] = true; |
| 55 | + q.offer(to); |
| 56 | + } |
| 57 | + to = moveLeft(cur); |
| 58 | + if (to != null && !visited[to.i][to.j][to.d]) { |
| 59 | + visited[to.i][to.j][to.d] = true; |
| 60 | + q.offer(to); |
| 61 | + } |
| 62 | + to = moveRight(cur); |
| 63 | + if (to != null && !visited[to.i][to.j][to.d]) { |
| 64 | + visited[to.i][to.j][to.d] = true; |
| 65 | + q.offer(to); |
| 66 | + } |
| 67 | + to = rotate(cur); |
| 68 | + if (to != null && !visited[to.i][to.j][to.d]) { |
| 69 | + visited[to.i][to.j][to.d] = true; |
| 70 | + q.offer(to); |
| 71 | + } |
| 72 | + } |
| 73 | + cnt ++; |
| 74 | + } |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + static Position moveUp(Position p) { |
| 79 | + if (p.d == col) { |
| 80 | + if (map[p.i-1][p.j] == '1') return null; |
| 81 | + } else if (map[p.i-1][p.j] == '1' || map[p.i-1][p.j+1] == '1' || map[p.i-1][p.j+2] == '1') return null; |
| 82 | + return new Position(p.i-1, p.j, p.d); |
| 83 | + } |
| 84 | + |
| 85 | + static Position moveDown(Position p) { |
| 86 | + if (p.d == col) { |
| 87 | + if (map[p.i+3][p.j] == '1') return null; |
| 88 | + } else if (map[p.i+1][p.j] == '1' || map[p.i+1][p.j+1] == '1' || map[p.i+1][p.j+2] == '1') return null; |
| 89 | + return new Position(p.i+1, p.j, p.d); |
| 90 | + } |
| 91 | + |
| 92 | + static Position moveLeft(Position p) { |
| 93 | + if (p.d == row) { |
| 94 | + if (map[p.i][p.j-1] == '1') return null; |
| 95 | + } else if (map[p.i][p.j-1] == '1' || map[p.i+1][p.j-1] == '1' || map[p.i+2][p.j-1] == '1') return null; |
| 96 | + return new Position(p.i, p.j-1, p.d); |
| 97 | + } |
| 98 | + |
| 99 | + static Position moveRight(Position p) { |
| 100 | + if (p.d == row) { |
| 101 | + if (map[p.i][p.j+3] == '1') return null; |
| 102 | + } else if (map[p.i][p.j+1] == '1' || map[p.i+1][p.j+1] == '1' || map[p.i+2][p.j+1] == '1') return null; |
| 103 | + return new Position(p.i, p.j+1, p.d); |
| 104 | + } |
| 105 | + |
| 106 | + static Position rotate(Position p) { |
| 107 | + if (p.d == col) { |
| 108 | + if (map[p.i][p.j-1] == '1' || map[p.i][p.j+1] == '1' || |
| 109 | + map[p.i+1][p.j-1] == '1' || map[p.i+1][p.j+1] == '1' || |
| 110 | + map[p.i+2][p.j-1] == '1' || map[p.i+2][p.j+1] == '1') { |
| 111 | + return null; |
| 112 | + } |
| 113 | + return new Position(p.i+1, p.j-1, row); |
| 114 | + } else { |
| 115 | + if (map[p.i-1][p.j] == '1' || map[p.i+1][p.j] == '1' || |
| 116 | + map[p.i-1][p.j+1] == '1' || map[p.i+1][p.j+1] == '1' || |
| 117 | + map[p.i-1][p.j+2] == '1' || map[p.i+1][p.j+2] == '1') { |
| 118 | + return null; |
| 119 | + } |
| 120 | + return new Position(p.i-1, p.j+1, col); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + static Position setPosition(Position p, int i, int j) { |
| 125 | + if (p == null) p = new Position(i, j, 0); |
| 126 | + else if (p.j == j) p.d = col; |
| 127 | + else p.d = row; |
| 128 | + return p; |
| 129 | + } |
| 130 | + |
| 131 | + static class Position { |
| 132 | + int i, j, d; |
| 133 | + |
| 134 | + public Position(int i, int j, int d) { |
| 135 | + this.i = i; |
| 136 | + this.j = j; |
| 137 | + this.d = d; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public boolean equals(Object o) { |
| 142 | + Position p = (Position) o; |
| 143 | + return i == p.i && j == p.j && d == p.d; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments