알고리즘 문제/BOJ
4963번 섬의 개수
parkit
2018. 9. 19. 19:24
728x90
반응형
C언어로 구현하였다.
전형적인 DFS의 연결 요소의 개수를 구하는 문제이다.
https://www.acmicpc.net/problem/4963
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 | #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> int w = 0, h = 0; int visit[51][51] = { -1, }; int map[51][51] = { 0, }; int dy[8] = { -1, 0, 1, 0, 1, -1, 1, -1}; int dx[8] = { 0, -1, 0, 1, 1, 1, -1, -1}; void DFS(int r, int c) { visit[r][c] = 1; // 방문 표시(true) for (int i = 0; i < 8; i++) { int y = r + dy[i]; int x = c + dx[i]; if (0 <= y && y < h && 0 <= x && x < w && map[y][x] == 1 && visit[y][x] == -1) { DFS(y, x); } } } int main(void) { int icnt = 0, scnt = 0; while (1) { memset(visit, -1, sizeof(visit)); memset(map, 0, sizeof(map)); scanf("%d %d", &w, &h); if (w == 0 && h == 0) break; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { scanf("%d", &map[i][j]); } } int result = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[i][j] == 1 && visit[i][j] == -1) // 땅(1)이고, 방문하지 않은 곳이라면 { DFS(i, j); ++result; } } } printf("%d\n", result); } return 0; } | cs |
728x90
반응형