Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

혼종 꼬지마루

[백준] 2667 - 단지번호 붙이기 본문

Algorithms/BFS

[백준] 2667 - 단지번호 붙이기

꼬지마루 2018. 10. 17. 23:09

일단 이 문제는 DFS, BFS 둘 다 가능하지만, 저는 BFS를 선호하기 때문에 BFS로 풀었습니다


1. 입력된 맵정보를 2차원 for문으로 순회하면서 맵이 1인 부분에서 BFS로 영역을 탐색


2. BFS 탐색이 한번 끝날 때 마다 카운트를 올려주면서 영역의 갯수를 구해주면 끝


ICT코테는 영역의 넓이를 구하라고 되어 있는데 이것도 BFS 내부에서 마킹할 때 마다 카운트를 세주면 되는 간단한 문제!


그래도 혹시 모르니 DFS, BFS문제 모두 올리겠습니다


- 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
69
#include <iostream>
#include <stdio.h>
#include <algorithm>
#define MX 30
 
using namespace std;
 
struct kkk {
    int x, y;
}q[MX * MX];
 
int map[MX][MX], check[MX][MX], arr[MX * MX];
int dx[] = { 00-1};
int dy[] = { -110};
int N, num;
 
void bfs(int x, int y)
{
    int st, ed, ix, iy, nx, ny;
    st = ed = -1;
    q[++st].x = x; q[st].y = y;
    check[y][x] = num;
 
    while (st != ed)
    {
        ix = q[++ed].x; iy = q[ed].y;
        for (int i = 0; i < 4; i++)
        {
            nx = ix + dx[i]; ny = iy + dy[i];
            if (nx < || ny < || nx >= N || ny >= N) continue;
            if (!map[ny][nx] || check[ny][nx]) continue;
            q[++st].x = nx; q[st].y = ny; check[ny][nx] = num;
        }
    }
}
 
int main(void)
{
    cin >> N;
 
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            scanf("%1d"&map[i][j]);
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (!check[i][j] && map[i][j])
            {
                num++;
                bfs(j, i);
            }
        }
    }
 
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            if (check[i][j]) arr[check[i][j]] += 1;
 
    sort(arr, arr + (num + 1));
 
    cout << num << endl;
 
    for (int i = 1; i <= num; i++)
        cout << arr[i] << endl;
 
    return 0;
}
cs

- 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
#define CRT_SECURE_NO_WARINGS
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#define MX 30
 
using namespace std;
 
int map[MX][MX], check[MX][MX];
int M, num[MX*MX], d = 1;
int dx[] = { -110};
int dy[] = { 00-1};
 
void dfs(int x, int y, int d)
{
    if (map[y][x] == || check[y][x] != 0return;
 
    check[y][x] = d; num[d]++;
 
    for (int i = 0; i < 4; i++)
    {
        if (x + dx[i] <= || y + dy[i] <= || x + dx[i] > M || y + dy[i] > M) continue;
        if (check[y + dy[i]][x+dx[i]] != 0continue;
        dfs(x + dx[i], y + dy[i], d);
    }
}
 
int main(void)
{
    cin >> M;
 
    for (int i = 1; i <= M; i++)
    {
        for (int j = 1; j <= M; j++scanf("%1d"&map[i][j]);
    }
 
    for (int i = 1; i <= M; i++)
    {
        for (int j = 1; j <= M; j++)
        {
            if (map[i][j] == && check[i][j] == 0) { dfs(j, i, d); d++; }
            else continue;
        }
    }
     
    sort(num, num + d);
 
    printf("%d\n", d - 1);
 
    if (d == && num[1== 0) { return 0; }
    else for (int i = 1; i < d; i++printf("%d\n", num[i]);
     
    return 0;
}
 
cs

'Algorithms > BFS' 카테고리의 다른 글

[백준] 16234 - 인구 이동  (0) 2018.11.08
[백준] 16236 - 아기 상어  (0) 2018.11.08
[SWEA] 5521 - 상원이의 생일파티  (0) 2018.09.07
[SWEA] 1267 - 작업순서  (0) 2018.09.03
[백준] 1938 - 통나무 옮기기  (0) 2018.08.30