[BaekJoon] 2667번 - 단지번호붙이기 [Java][C++]
[BaekJoon] 2667번 - 단지번호붙이기 [Java][C++]
1. 아이디어
주어진 지도의 모든 좌표를 순회하며 집이 있는 곳이면 단지의 크기를 구하고 개수를 세주면 된다. 단지의 크기는 BFS나, DFS를 활용해서 구하면 되고 이를 오름차순으로 정렬해서 출력해줬다.
2. 코드
1. BFS [Java]
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
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n;
static char[][] grid;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
grid = new char[n][n];
for (int i = 0; i < n; i++) {
grid[i] = br.readLine().toCharArray();
}
int cnt = 0;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
cnt++;
list.add(bfs(i, j));
}
}
}
list.sort(Comparator.naturalOrder());
sb.append(cnt).append("\n");
for (int x : list) {
sb.append(x).append("\n");
}
System.out.println(sb);
}
static int bfs(int sr, int sc) {
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[]{sr, sc});
grid[sr][sc] = '0';
int cnt = 1;
while (!q.isEmpty()) {
int[] cur = q.poll();
for (int d = 0; d < 4; d++) {
int nr = cur[0] + dr[d];
int nc = cur[1] + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (grid[nr][nc] == '0') continue;
q.offer(new int[]{nr, nc});
grid[nr][nc] = '0';
cnt++;
}
}
return cnt;
}
}
2. BFS [C++]
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
#include <bits/stdc++.h>
using namespace std;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n;
char grid[26][26];
int bfs(int sr, int sc) {
queue<pair<int, int>> q;
q.push({sr, sc});
grid[sr][sc] = '0';
int cnt = 1;
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (grid[nr][nc] == '0') continue;
q.push({nr, nc});
grid[nr][nc] = '0';
cnt++;
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
int cnt = 0;
vector<int> v;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
cnt++;
v.push_back(bfs(i, j));
}
}
}
sort(v.begin(), v.end());
cout << cnt << '\n';
for (int x : v) {
cout << x << '\n';
}
}
3. DFS [Java]
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
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n;
static char[][] grid;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
n = Integer.parseInt(br.readLine());
grid = new char[n][n];
for (int i = 0; i < n; i++) {
grid[i] = br.readLine().toCharArray();
}
int cnt = 0;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
cnt++;
list.add(dfs(i, j));
}
}
}
list.sort(Comparator.naturalOrder());
sb.append(cnt).append("\n");
for (int x : list) {
sb.append(x).append("\n");
}
System.out.println(sb);
}
static int dfs(int r, int c) {
grid[r][c] = '0';
int cnt = 1;
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (grid[nr][nc] == '0') continue;
cnt += dfs(nr, nc);
}
return cnt;
}
}
4. DFS [C++]
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
#include <bits/stdc++.h>
using namespace std;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n;
char grid[26][26];
int dfs(int r, int c) {
grid[r][c] = '0';
int cnt = 1;
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
if (grid[nr][nc] == '0') continue;
cnt += dfs(nr, nc);
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
int cnt = 0;
vector<int> v;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
cnt++;
v.push_back(dfs(i, j));
}
}
}
sort(v.begin(), v.end());
cout << cnt << '\n';
for (int x : v) {
cout << x << '\n';
}
}
3. 디버깅
없음.
4. 참고
없음.
This post is licensed under CC BY 4.0 by the author.