반응형

문제 링크: https://www.acmicpc.net/problem/16932

 

16932번: 모양 만들기

N×M인 배열에서 모양을 찾으려고 한다. 배열의 각 칸에는 0과 1 중의 하나가 들어있다. 두 칸이 서로 변을 공유할때, 두 칸을 인접하다고 한다. 1이 들어 있는 인접한 칸끼리 연결했을 때, 각각의

www.acmicpc.net

<문제 풀이> BFS

1. 1로 이루어진 연결 요소를 구해서 그룹화한다.

-> 그룹화는 각각의 연결 요소에 번호를 부여

 

2. 배열에서 0을 찾아서 (상하좌우 영역의 크기 + 1 ( 0을 1로 바꾼 경우))의 최댓값을 구한다.

-> 이때 같은 상하좌우를 확인할 때 같은 영역은 중복 체크하지 않도록 check 배열을 하나 둬야 한다.

<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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<queue>
#include<algorithm>
#include<utility>
using namespace std;
 
#define X first
#define Y second
 
int N, M;
 
const int dx[4= { 001-1 };
const int dy[4= { 1-100 };
 
 
int board[1000][1000];
bool visited[1000][1000];
bool check[1000000];
int area[1000000];
int res = 0;
int num = 1;
 
int dfs(int cx, int cy) {
    int ret = 1;
    for (int dir = 0; dir < 4; dir++) {
        int nx = cx + dx[dir];
        int ny = cy + dy[dir];
        if (nx < 0 || ny < 0 || nx >= N || ny >= M)continue;
        if (board[nx][ny] != 0 && !visited[nx][ny]) {
            board[nx][ny] = num;
            visited[nx][ny] = true;
            ret += dfs(nx, ny);
        }
    }
    return ret;
}
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> board[i][j];
        }
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (board[i][j] == 1 && !visited[i][j]) {
                visited[i][j] = true;
                board[i][j] = num;
                area[num] = dfs(i, j);
                num++;
            }
        }
    }
    
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (board[i][j] == 0) {
                int temp = 1;
                for (int dir = 0; dir < 4; dir++) {
                    int nx = i + dx[dir];
                    int ny = j + dy[dir];
                    if (nx < 0 || ny < 0 || nx >= N || ny >= M)continue;
                    if (board[nx][ny] != 0 && !check[board[nx][ny]]){
                        temp += area[board[nx][ny]];
                        check[board[nx][ny]] = true;
                    }
                    
                }
                for (int dir = 0; dir < 4; dir++) {
                    int nx = i + dx[dir];
                    int ny = j + dy[dir];
                    if (nx < 0 || ny < 0 || nx >= N || ny >= M)continue;
                    if (board[nx][ny] != 0) {
                        check[board[nx][ny]] = false;
                    }
 
                };
                res = max(res, temp);
            }
        }
    }
    cout << res;
    return 0;
}
cs
반응형

'알고리즘 문제풀이 > 백준' 카테고리의 다른 글

[백준 13460번] 구슬 탈출 2  (0) 2022.02.10
[백준 16973번] 직사각형 탈출  (0) 2022.01.31
[백준 3190번] 뱀  (0) 2022.01.19
[백준 2638번] 치즈  (0) 2022.01.17
[백준 16985번] Maaaaaaaaaze  (0) 2022.01.16

+ Recent posts