|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public Main { |
| 6 | + static class Point { |
| 7 | + int r; |
| 8 | + int c; |
| 9 | + public Point(int r, int c) { |
| 10 | + this.r = r; |
| 11 | + this.c = c; |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + static int[] dr = {0, 0, -1, 1}; |
| 16 | + static int[] dc = {-1, 1, 0, 0}; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 20 | + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 21 | + |
| 22 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 23 | + int N = Integer.parseInt(st.nextToken()); |
| 24 | + int M = Integer.parseInt(st.nextToken()); |
| 25 | + |
| 26 | + // 스위치 정보 저장 |
| 27 | + List<Point>[][] buttons = new ArrayList[N+1][N+1]; |
| 28 | + for (int i = 1; i < N+1; i++) { |
| 29 | + for (int j = 1; j < N+1; j++) { |
| 30 | + buttons[i][j] = new ArrayList<>(); |
| 31 | + } |
| 32 | + } |
| 33 | + for (int i = 0; i < M; i++) { |
| 34 | + st = new StringTokenizer(br.readLine()); |
| 35 | + int r = Integer.parseInt(st.nextToken()); |
| 36 | + int c = Integer.parseInt(st.nextToken()); |
| 37 | + int nr = Integer.parseInt(st.nextToken()); |
| 38 | + int nc = Integer.parseInt(st.nextToken()); |
| 39 | + buttons[r][c].add(new Point(nr, nc)); |
| 40 | + } |
| 41 | + |
| 42 | + // light[i][j] = (i, j) 방에 불이 켜짐 |
| 43 | + boolean[][] light = new boolean[N+1][N+1]; |
| 44 | + light[1][1] = true; |
| 45 | + // visited[i][j] = (i, j)방에 도착함 |
| 46 | + boolean[][] visited = new boolean[N+1][N+1]; |
| 47 | + visited[1][1] = true; |
| 48 | + |
| 49 | + Queue<Point> q = new ArrayDeque<>(); |
| 50 | + q.offer(new Point(1, 1)); |
| 51 | + |
| 52 | + // (i, j)로 움직일 수 있는 조건은 |
| 53 | + // light[i][j]가 참이면서 visited[i][j]가 참인 지점! |
| 54 | + while (!q.isEmpty()) { |
| 55 | + Point p = q.poll(); |
| 56 | + |
| 57 | + // p 지점에서 켤 수 있는 스위치를 모두 켠다 |
| 58 | + for (Point np : buttons[p.r][p.c]) { |
| 59 | + |
| 60 | + if (light[np.r][np.c]) { continue; } |
| 61 | + |
| 62 | + light[np.r][np.c] = true; // 스위치 켬 |
| 63 | + |
| 64 | + // 이전에 방문한 적이 있는데 이번에 불이 켜졌다면 도달 가능한 지점! |
| 65 | + if (visited[np.r][np.c]) { |
| 66 | + q.offer(np); // 큐에 삽입 |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (int i = 0; i < 4; i++) { |
| 71 | + |
| 72 | + int nr = p.r + dr[i]; |
| 73 | + int nc = p.c + dc[i]; |
| 74 | + |
| 75 | + if (1 <= nr && nr < N+1 && 1 <= nc && nc < N+1) { |
| 76 | + if (visited[nr][nc]) { continue; } |
| 77 | + |
| 78 | + // 방문 표시 |
| 79 | + visited[nr][nc] = true; |
| 80 | + // 만약 불이 켜져 있는 곳이라면 큐에 삽입! |
| 81 | + if (light[nr][nc]) { |
| 82 | + q.offer(new Point(nr, nc)); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + int answer = 0; |
| 89 | + for (int i = 1; i < N+1; i++) { |
| 90 | + for (int j = 1; j < N+1; j++) { |
| 91 | + if (light[i][j]) { answer++; } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + bw.write(answer + "\n"); |
| 96 | + |
| 97 | + |
| 98 | + br.close(); |
| 99 | + bw.flush(); |
| 100 | + bw.close(); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
0 commit comments