2630 색종이 만들기
2019. 7. 25. 19:05ㆍ알고리즘/백준
재귀호출을 묻는 문제
문제: https://www.acmicpc.net/problem/2630
깃허브주소: https://github.com/surinoel/boj/blob/master/2630.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int mat[128][128]; | |
void go(int n, int x, int y, int &b, int &w) { | |
if (n == 1) { | |
if (mat[x][y] == 1) b += 1; | |
else w += 1; | |
return; | |
} | |
bool ok = true; | |
int target = mat[x][y]; | |
for (int i = x; i < x + n; i++) { | |
for (int j = y; j < y + n; j++) { | |
if (target != mat[i][j]) { | |
ok = false; | |
} | |
} | |
} | |
if (ok) { | |
if (target == 1) b += 1; | |
else w += 1; | |
} | |
else { | |
go(n / 2, x, y, b, w); | |
go(n / 2, x + n / 2, y, b, w); | |
go(n / 2, x, y + n / 2, b, w); | |
go(n / 2, x + n / 2, y + n / 2, b, w); | |
} | |
return; | |
} | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
for (int i = 0; i < n; i++) { | |
for (int j = 0; j < n; j++) { | |
cin >> mat[i][j]; | |
} | |
} | |
int b = 0, w = 0; | |
go(n, 0, 0, b, w); | |
cout << w << '\n' << b << '\n'; | |
return 0; | |
} |
'알고리즘 > 백준' 카테고리의 다른 글
9517 아이 러브 크로아티아 (0) | 2019.07.26 |
---|---|
1059 수2 (0) | 2019.07.25 |
12100 2048(Easy) (0) | 2019.07.24 |
16947 서울 지하철 2호선 (1) | 2019.07.23 |
4659 비밀번호 발음하기 (0) | 2019.07.21 |