반응형
https://www.acmicpc.net/problem/4963
#include <iostream>
#include <string.h>
using namespace std;
int island_map[51][51];
int dir[2][8] = {{0, 1, 1, 1, 0, -1, -1, -1},
{1, 1, 0, -1, -1, -1, 0, 1}};
int width, height;
bool isRight(int newX, int newY) {
return ((newX > -1 && newX < height) && (newY > -1 && newY < width));
}
void dfs(int x, int y) {
int newX = 0;
int newY = 0;
for(int i = 0; i < 8; i++) {
newX = x + dir[0][i];
newY = y + dir[1][i];
if(isRight(newX, newY) && island_map[newX][newY] == 1) {
island_map[newX][newY] = 0;
dfs(newX, newY);
}
}
}
int main() {
int island_cnt = 0;
while(true) {
cin >> width >> height;
if(width == 0 && height == 0) return 0;
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
cin >> island_map[i][j];
}
}
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
if(island_map[i][j] == 1) {
island_map[i][j] = 0;
dfs(i, j);
island_cnt++;
}
}
}
cout << island_cnt << "\n";
island_cnt = 0;
memset(island_map, 0, sizeof(island_map[0]));
memset(island_map, 0, sizeof(island_map[1]));
}
return 0;
}
'Development. > Problem solving.' 카테고리의 다른 글
[BAEKJOON] 1157 - 단어 공부 (0) | 2020.08.17 |
---|---|
[BAEKJOON] 2675 - 문자열 반복 (0) | 2020.08.17 |
[BAEKJOON] 2884 - 알람 시계 (0) | 2020.08.17 |
[BAEKJOON] 2609 - 최대공약수와 최소공배수 (0) | 2020.08.17 |
[BAEKJOON] 1822 - 차집합 (0) | 2020.08.17 |