본문 바로가기
SWE/코테

[성실코딩 9일차] 백준 #1780 종이의 개수

by S나라라2 2018. 10. 23.
반응형

왜 코테 다~ 보고 나니까 이런 문제가 많이 접해지는걸까욥 

글고 왜 쉬운걸까욥 허허허허허허


아직도 DFS, BFS 구조 잘 모르고 푸는 느낌입니다 ㅜㅜ 문제 접하면 이게 어떤 알고리즘 사용해서 푸는거구나 느낌이 안옵니다ㅠㅠ



#include<iostream>
#include<vector>

using namespace std;

int n;
vector<vector<int>> table;
int cnt[3] = { 0, }; 
// cnt[0] : -1로 채워진 종이 갯수 
// cnt[1] : 0
// cnt[2] : 1


// 종이의 모든 숫자가 같은지 확인하는 함수
// 같을 경우 return true;
bool AllSameNumber(int _x, int _y, int n) {
	int num = table[_x][_y];
	for (int i = _x; i < _x + n; i++) {
		for (int j = _y; j < _y + n; j++) {
			if (table[i][j]!=num) {
				return false;
			}
		}
	}
	return true;
}

void func(int _x, int _y, int n) { // 시작좌표 x,y 한 변 사이즈 n
	if (n==1) { // 탈출 조건
		// count
		if (table[_x][_y]==-1) {
			cnt[0]++;
		}
		else if (table[_x][_y] == 0) {
			cnt[1]++;
		}
		else {
			cnt[2]++;
		}
		return;
	}
	if (AllSameNumber(_x,_y,n)) { // 채워진 수가 모두 같을 경우
								  // 탈출조건
		// count
		if (table[_x][_y] == -1) {
			cnt[0] ++;
		}
		else if (table[_x][_y] == 0) {
			cnt[1] ++;
		}
		else {
			cnt[2] ++;
		}
		return;
	}
	else { // 채워진 수 다를 경우
		// 가로, 세로 3으로 나눠서 
		// 재귀 호출하기
		int new_size = n / 3;
		for (int i = _x; i < _x+n; i+=new_size) {
			for (int j = _y; j < _y+n;  j+=new_size) {
				func(i,j,new_size);
			}
		}
	}
}

int main() {

	// 입력 받기
	cin >> n;
	table.assign(n, vector<int>(n,0));  // table[n][n]
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> table[i][j];
		}
	}
	
	// 연산
	func(0,0,n);
	// -1,0,1로 채워진 종이 갯수 세기!

	// 출력
	for (int i = 0; i < 3; i++) {
		cout << cnt[i] << endl;
	}

	return 0;
}
반응형