PS/백준 알고리즘[BOJ]

[백준 2630번] 색종이 만들기 (C++)

BE_개발자 2023. 10. 20. 22:00
728x90
반응형

예시 이미지

//white: 0, black: 1
#define SIZE 128
#define F first
#define S second
#define X dir_x
#define Y dir_y
#include<iostream>

using namespace std;
int col;
int cnt[2];  //index 0: white, 1:blue
int map[SIZE][SIZE];
// change - true, not change - false
bool check(int x1, int y1, int x2, int y2) {
	col = map[x1][y1];
	for (int i = x1; i < x2; i++) {
		for (int j = y1; j < y2; j++) {
			if (col != map[i][j]) return true;
		}
	}
	return false;
}

void dc(int size, int x, int y) {
	if (size == 1) {
		cnt[map[x][y]]++;// cnt[0]: white, cnt[1]: blue
		return;
	}
	if (!check(x, y, x + size, y + size)) {
		cnt[map[x][y]]++;
		return;
	}
	size /= 2;
	pair<int, int> dir_x[4] = { {x, x + size}, {x + size, x + 2 * size}, {x, x + size}, {x + size, x + 2 * size} };
	pair<int, int> dir_y[4] = { {y, y + size}, {y, y + size}, {y + size, y + 2 * size}, {y + size, y + 2 * size} };
	for (int i = 0; i < 4; i++) {
		dc(size, X[i].F, Y[i].F);
	}
}

int main(void) {
	cin.tie(0);
	ios_base::sync_with_stdio(NULL);
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) cin >> map[i][j];
	}
	dc(N, 0, 0);
	cout << cnt[0] << "\n" << cnt[1];

	return 0;
}

 

 

//white: 0, blue: 1
#define SIZE 128
#include<iostream>

using namespace std;
int col;
int cnt[2];  //index 0: white, 1:blue
int map[SIZE][SIZE];
// change - true, not change - false
bool check(int x1, int y1, int x2, int y2) {
	col = map[x1][y1];
	for (int i = x1; i < x2; i++) {
		for (int j = y1; j < y2; j++) {
			if (col != map[i][j]) return true;
		}
	}
	return false;
}

void dc(int size, int x, int y) {
	if (size == 1) {
		cnt[map[x][y]]++;// cnt[0]: white, cnt[1]: blue
		return;
	}
	if (!check(x, y, x + size, y + size)) {
		cnt[map[x][y]]++;
		return;
	}
	size /= 2;
	dc(size, x, y);
	dc(size, x + size, y);
	dc(size, x, y + size);
	dc(size, x + size, y + size);
}

int main(void) {
	cin.tie(0);
	ios_base::sync_with_stdio(NULL);
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) cin >> map[i][j];
	}
	dc(N, 0, 0);
	cout << cnt[0] << "\n" << cnt[1];

	return 0;
}
728x90
반응형