본문 바로가기

알고리즘/BOJ

2667번

2667번 - 단지번호붙이기


BFS 나 DFS 를 이용해서 각 구역에 대한 값들만 잘 지정해주면 되는 간단한 문제였다.


하지만 입력값을 평소처럼 int 형으로 받는게 아니라 


나 같은 경우에는 char 형으로 받아서 변환시켜서 넣어주었다.


입력하는 것에서 약간 당황스러웠지만, 좋은 경험이었다.


check 배열을 통해 방문하지 않은 곳은 0으로 , 방문한 곳은 그 구역에 따른 번호를 붙여주었다.





<정답 코드 - BFS , 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
 
int dx[4= { 0,0,1,-1 };
int dy[4= { 1,-1,0,0 };
int map[26][26];
int check[26][26];
int cnt;
int N;
vector<int> v;
void bfs()
{
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (map[i][j] == 1 && check[i][j] == 0)
            {
                cnt++;
 
                queue<pair<intint>> q;
                q.push(make_pair(i, j));
                check[i][j] = cnt;
 
                while (!q.empty())
                {
                    int now_x = q.front().first;
                    int now_y = q.front().second;
                    q.pop();
 
                    for (int d = 0; d < 4; d++)
                    {
                        int next_x = now_x + dx[d];
                        int next_y = now_y + dy[d];
                        if (next_x >= 0 && next_y >= 0 && next_x < N && next_y < N && check[next_x][next_y] == 0 && map[next_x][next_y]==1)
                        {
                            check[next_x][next_y] = cnt;
                            q.push(make_pair(next_x, next_y));
                        }
                    }
                }
            }
        }
    }
}
void dfs(int now_x,int now_y)
{
    check[now_x][now_y] = cnt;
 
    for (int d = 0; d < 4; d++)
    {
        int next_x = now_x + dx[d];
        int next_y = now_y + dy[d];
        if (next_x >= 0 && next_y >= 0 && next_x < N && next_y < N && check[next_x][next_y] == 0 && map[next_x][next_y] == 1)
        {
            dfs(next_x, next_y);
        }
    }
}
int main()
{
    
    cin >> N;
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            char a;
            cin >> a;
            map[i][j] = a - '0';
        }
    }
    //bfs
    bfs();
 
 
 
    //dfs
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (map[i][j] == 1 && check[i][j] == 0)
            {
                cnt++;
                dfs(i, j);
            }
 
        }
    }
    
 
    for (int k = 1; k <= cnt; k++)
    {
        int num = 0;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (check[i][j] == k)
                {
                    num += 1;
                }
            }
        }
        v.push_back(num);
    }
 
    sort(v.begin(), v.end());
 
    cout << cnt << endl;
 
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << endl;
    }
 
}
cs


반응형

'알고리즘 > BOJ' 카테고리의 다른 글

7576번  (0) 2017.11.23
4963번  (0) 2017.11.23
2331번  (0) 2017.11.22
10451번  (0) 2017.11.22
1707번  (0) 2017.11.22