[PS] 99ํด๋ฝ ์ฝํ ์คํฐ๋ 18์ผ์ฐจ TIL (๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ)
ํ๊ทธ: 99ํด๋ฝ, BFS/DFS, PS, TIL, ์ฝ๋ฉํ ์คํธ์ค๋น, ํญํด99
์นดํ ๊ณ ๋ฆฌ: PS
![]()
๋ฌธ์

์ค๋ช
์ด๋ฒ ๋ฌธ์ ๋ ๊ทธ๋ํ์ ๋ํ ์ ๋ณด๋ง ์ฃผ์ด์ง๊ณ ๊ทธ ๊ทธ๋ํ๋ฅผ ํ์ํ๋ ๊ฒ์ด ์๋๋ผ ์ฃผ์ด์ง ์
๋ ฅ๊ฐ์์ ๊ทธ๋ํ ํ์์ด ์ด๋ฃจ์ด์ ธ์ผ ํ ๋ถ๋ถ์ ์ฐพ์์ผ ํ๋ค.
์๋ ๊ทธ๋ฆผ์ฒ๋ผ ๋งจ ์ฒ์ ํญ๋ชฉ๋ถํฐ 1์ด ์ ํ ๋ถ๋ถ์ ํ์ํด์ผ ํ๋ค.

1 ๋ถ๋ถ์ ๋ง๋๋ฉด ์ด๋ํ ์ ์๋ ๊ฒฝ๋ก๋ ์,ํ,์ข,์ฐ ์์์ ๋ํด ๋ค์ ํ์์ ์ํํ๋ค. ์ด๋ dx dy ๋ฐฉ์์ผ๋ก ๋ฐฉํฅ์ ๋ํ ์์น๋ฅผ ๋ฐฐ์ด์ ๋ฏธ๋ฆฌ ์ ์ฅํด ๋๊ณ ์ฌ์ฉํ๋ฉด ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง๋ค.
1
2
3
4
5
6
7
8
static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // ์ํ์ข์ฐ
...
// ์ํ์ข์ฐ ํ์
for (int i = 0; i < pos.length; i++){
// ๋ฐฉํฅ ์ด๋ ํ ์ขํ
int nx = x + pos[i][0];
int ny = y + pos[i][1];
}
dfs ํจ์ ๋ด์์ ์,ํ,์ข,์ฐ ์ขํ๋ก ๊ณ์ ๊ฐฑ์ ํ๋ฉด์ ๊ฐ๊ฐ์ ์์์์ ๋ค์ dfs ํ๋ค. ์ด๋ ์ด๋ ํ์๋์ ์ขํ ์ธ๋ฑ์ค๊ฐ graph ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ฒ์ ๋ด์ ์์ ๊ฒฝ์ฐ์๋ง ํ์ํ๋๋ก ํ๋ค.
1
2
3
4
// ์ด๋ํ ๊ฒฝ๋ก๊ฐ graph ๋ฐฐ์ด ๋ฒ์ ์์ ์๋์ง ๊ฒ์ฌ
private static boolean isRange(int nx, int ny) {
return nx >= 0 && nx < N && ny >= 0 && ny < N;
}
ํ์ด (DFS)
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
public class Main {
static int[][] graph;
static boolean[][] visited;
static int N;
static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // ์ํ์ข์ฐ
static int count;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
graph = new int[N][N];
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
String s = br.readLine();
for (int j = 0; j < N; j++) {
graph[i][j] = s.charAt(j) - '0';
}
}
ArrayList<Integer> result = new ArrayList<>();
// (0,0) ๋ถํฐ ๋ชจ๋ ์์์ ๋ํด dfs
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && graph[i][j] == 1) {
count = 0;
dfs(i, j);
result.add(count);
}
}
}
System.out.println(result.size());
Collections.sort(result);
for (Integer integer : result) {
System.out.println(integer);
}
}
private static void dfs(int x, int y) {
visited[x][y] = true;
count++;
// ์ํ์ข์ฐ ํ์
for (int i = 0; i < pos.length; i++) {
int nx = x + pos[i][0];
int ny = y + pos[i][1];
if (isRange(nx, ny) && !visited[nx][ny] && graph[nx][ny] == 1) {
dfs(nx, ny);
}
}
}
private static boolean isRange(int nx, int ny) {
return nx >= 0 && nx < N && ny >= 0 && ny < N;
}
}
ํ์ด (BFS)
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
public class Main {
static int[][] graph;
static boolean[][] visited;
static int N;
static int[][] pos = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // ์ํ์ข์ฐ
static int count;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
graph = new int[N][N];
visited = new boolean[N][N];
for (int i = 0; i < N; i++) {
String s = br.readLine();
for (int j = 0; j < N; j++) {
graph[i][j] = s.charAt(j) - '0';
}
}
ArrayList<Integer> result = new ArrayList<>();
// (0,0) ๋ถํฐ ๋ชจ๋ ์์์ ๋ํด dfs
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visited[i][j] && graph[i][j] == 1) {
count = 1;
bfs(i, j);
result.add(count);
}
}
}
System.out.println(result.size());
Collections.sort(result);
for (Integer integer : result) {
System.out.println(integer);
}
}
private static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[] {x, y});
visited[x][y] = true;
while (!queue.isEmpty()) {
int[] current = queue.poll();
int cx = current[0];
int cy = current[1];
// ์ํ์ข์ฐ ํ์
for (int i = 0; i < pos.length; i++) {
int nx = cx + pos[i][0];
int ny = cy + pos[i][1];
if (isRange(nx, ny) && !visited[nx][ny] && graph[nx][ny] == 1) {
visited[nx][ny] = true;
queue.offer(new int[] {nx, ny});
count++;
}
}
}
}
private static boolean isRange(int nx, int ny) {
return nx >= 0 && nx < N && ny >= 0 && ny < N;
}
}
๋๊ธ๋จ๊ธฐ๊ธฐ