[백준 4963번] 섬의 개수

2021. 11. 25. 04:29·알고리즘 문제풀이/백준
반응형

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

 

4963번: 섬의 개수

입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은 양의 정수이다. 둘째 줄부터 h개 줄에는 지도

www.acmicpc.net

 

<문제 풀이> BFS

8방향 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
65
66
67
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <utility>
#include <climits>
using namespace std;
 
#define X first 
#define Y second 
 
int w, h;
int board[50][50];
bool visited[50][50];
 
int dx[8] = {0 ,-1, -1, -1, 0, 1, 1, 1};
int dy[8] = { 1, 1, 0,-1,-1,-1, 0, 1};
 
int bfs() {
    queue<pair<int, int> > Q;
    int res = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (!visited[i][j] && board[i][j] == 1) {
                Q.push({ i, j });
                visited[i][j] = true;
                while (!Q.empty()) {
                    auto cur = Q.front(); Q.pop();
                    for (int dir = 0; dir < 8; dir++) {
                        int nx = cur.X + dx[dir];
                        int ny = cur.Y + dy[dir];
                        if (nx < 0 || ny < 0 || nx >= h || ny >= w)continue;
                        if (board[nx][ny] == 0 || visited[nx][ny])continue;
                        Q.push({ nx, ny });
                        visited[nx][ny] = true;
                    }
                }
                res++;
            }
        }
    }
    return res;
}
 
int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  
    while (true) {
        cin >> w >> h;
        if (w == 0 && h == 0) break;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin>> board[i][j];
            }
        }
        cout << bfs()<< '\n';
        fill(&board[0][0], &board[49][50], 0);
        fill(&visited[0][0], &visited[49][50], 0);
    }
 
 
    return 0;
}
 
Colored by Color Scripter
cs
반응형
저작자표시 (새창열림)

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

[백준 11967번] 불켜기  (0) 2021.12.04
[백준 3197번] 백조의 호수  (0) 2021.12.02
[백준 1600번] 말이 되고픈 원숭이  (0) 2021.11.25
[백준 17071번] 숨바꼭질 5  (0) 2021.11.25
[백준 12851번] 숨바꼭질2  (0) 2021.11.22
'알고리즘 문제풀이/백준' 카테고리의 다른 글
  • [백준 11967번] 불켜기
  • [백준 3197번] 백조의 호수
  • [백준 1600번] 말이 되고픈 원숭이
  • [백준 17071번] 숨바꼭질 5
슥지니
슥지니
개발 블로그
  • 슥지니
    슥지니의 코딩노트
    슥지니
  • 전체
    오늘
    어제
    • 분류 전체보기 (198)
      • 알고리즘 문제풀이 (158)
        • 백준 (158)
      • 알고리즘 (6)
      • Node.js (2)
        • MongoDB (1)
        • 기타 (1)
      • spring (0)
      • 가상화폐 (1)
        • 바이낸스(Binance) (1)
      • C++ 테트리스 게임 (0)
      • C++ (10)
      • 안드로이드 프로그래밍 (21)
        • 코틀린 (21)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    콘솔
    dp
    우선순위 큐
    BFS
    콘솔 테트리스 게임
    C
    구현
    백준
    그리디
    코틀린을 활용한 안드로이드 프로그래밍
    그래프
    코틀린
    Kotlin
    다이나믹 프로그래밍
    C++
    알고리즘
    자료구조
    백트랙킹
    시뮬레이션
    dfs
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
슥지니
[백준 4963번] 섬의 개수
상단으로

티스토리툴바