-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_11967.java
More file actions
82 lines (64 loc) · 1.83 KB
/
baekjoon_11967.java
File metadata and controls
82 lines (64 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.*;
import java.io.*;
public class baekjoon_11967 {
static List<int[]>[][] rooms;
static boolean[][] isLightsOn;
static int[] dr = {-1, 1, 0, 0};
static int[] dc = {0, 0, -1, 1};
static int N;
static int isOn;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
rooms = new ArrayList[N + 1][N + 1];
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
rooms[i][j] = new ArrayList<>();
}
}
isLightsOn = new boolean[N+1][N+1];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
rooms[x][y].add(new int[]{a, b});
}
isOn = 1;
bfs();
System.out.println(isOn);
}
static void bfs() {
Deque<int[]> q = new ArrayDeque<>();
boolean[][] visited = new boolean[N + 1][N + 1];
q.offer(new int[]{1, 1});
isLightsOn[1][1] = true;
visited[1][1] = true;
boolean flag = false; // 새로 불 킨 곳이 있는지
while (!q.isEmpty()) {
int[] cur = q.poll();
for (int[] next : rooms[cur[0]][cur[1]]) {
// 불이 off -> on
if (!isLightsOn[next[0]][next[1]]) {
isLightsOn[next[0]][next[1]] = true;
isOn++;
flag = true;
}
}
for (int i = 0; i < 4; i++) {
int nr = dr[i] + cur[0];
int nc = dc[i] + cur[1];
if (nr < 1 || nr > N || nc < 1 || nc > N) continue;
if (visited[nr][nc] || !isLightsOn[nr][nc]) continue;
q.offer(new int[] {nr, nc});
visited[nr][nc] = true;
}
}
if (flag) {
bfs();
}
}
}