반응형
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 | #include <iostream> #include <queue> using namespace std; int h,w,a[51][51], d[51][51], cnt, dh[] = { 0,0,-1,1,1,1,-1,-1 }, dw[] = { -1,1,0,0,-1,1,-1,1 }; void DFS(int H, int W, int cnt) { d[H][W] = cnt; for (int i = 0; i < 8; i++) { int nw = W + dw[i]; int nh = H + dh[i]; if (0<=nh && nh < h && 0<=nw && nw < w) if(a[nh][nw] == 1 && d[nh][nw] == 0) DFS(nh, nw, cnt); } } int main() { while (1) { cin >> w>>h; if (h == 0 && w == 0) { return 0; } cnt = 0; for (int i = 0; i < 51; i++) for (int j = 0; j < 51; j++) a[i][j] = d[i][j] = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> a[i][j]; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (a[i][j] == 1 && d[i][j] == 0) DFS(i, j, ++cnt); cout << cnt << '\n'; } } | cs |
'Algorithm' 카테고리의 다른 글
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 9012번:괄호 답 (0) | 2017.02.10 |
---|---|
(C++) - 백준(BOJ) 13241번 : 최소공배수 답 (0) | 2017.02.10 |
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 4963번:섬의 개수(BFS) 답 (0) | 2017.02.08 |
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 2693번:N번째 큰 수 답 (0) | 2017.02.08 |
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1932번:숫자삼각형 답 (0) | 2017.02.08 |