algorithm/BOJ

BOJ 4963번 섬의 개수

_JunHo 2020. 1. 14. 18:14

BOJ : https://www.acmicpc.net/problem/4963

GitHub : https://github.com/junho0956/Algorithm/blob/master/4963/4963/%EC%86%8C%EC%8A%A4.cpp

 

문제에서 요구하는 대로 구현해주면 문제될게 딱히 없는 문제이다.

** 동서남북뿐만아니라 대각선도 다봐야한다 **

더보기
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
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
#pragma warning(disable:4996)
 
vector<int> v;
queue<pair<intint> > q;
int mx[8= { -1,1,0,0,-1,1,1,-1 };
int my[8= { 0,0,1,-1,-1,-1,1,1 };
int arr[50][50];
int T;
 
int main() {
 
    while (1) {
        int w, h;
        scanf("%d%d"&w, &h);
        if (w == 0 && h == 0break;
 
        for (int i = 0; i < h; i++) {
            for (int k = 0; k < w; k++) {
                scanf("%d"&arr[i][k]);
            }
        }
 
        int cnt = 0;
        for (int i = 0; i < h; i++) {
            for (int k = 0; k < w; k++) {
                if (arr[i][k]) {
                    cnt++;
                    arr[i][k] = 0;
                    q.push({ i,k });
                    while (!q.empty()) {
                        int x = q.front().second;
                        int y = q.front().first;
                        q.pop();
 
                        for (int j = 0; j < 8; j++) {
                            int xx = mx[j] + x;
                            int yy = my[j] + y;
                            if (xx >= 0 && yy >= 0 && xx < w && yy < h && arr[yy][xx]) {
                                q.push({ yy,xx });
                                arr[yy][xx] = 0;
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n", cnt);
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter