πŸ‘Š PS/Algorithm

[Baekjoon] 2630번: 색쒅이 λ§Œλ“€κΈ°

hades1 2024. 7. 11. 18:13

πŸ₯… 문제

https://www.acmicpc.net/problem/2630

 

πŸ” 섀계

ν˜„μž¬ μƒ‰μ’…μ΄μ˜ λͺ¨λ“  칸의 색이 κ°™μ§€ μ•ŠμœΌλ©΄ 4λ“±λΆ„ν•΄ λ‚˜κ°€λŠ” λ¬Έμ œμ΄λ‹€. μž¬κ·€λ‘œ ν•΄κ²°ν•  λ•Œμ˜ μ‹œκ°„ λ³΅μž‘λ„λ₯Ό 계산해보면, O(2^7*2^7+2^6*2^6*2^2+2^5*2^5*2^2*2^2...)=O(2^14*8)=O(2^17)μ΄λ―€λ‘œ, μ‹œκ°„ μ œν•œ 1초 내에 ν•΄κ²°ν•  수 μžˆλ‹€.

 

μž¬κ·€ν•¨μˆ˜μ— μ‹œμž‘μ κ³Ό ν˜„μž¬ μƒ‰μ’…μ΄μ˜ λ³€μ˜ 길이λ₯Ό μ „λ‹¬ν•œλ‹€. 칸의 색이 ν•˜λ‚˜λΌλ„ λ‹€λ₯΄λ©΄ μž˜λΌμ•Ό ν•˜κΈ° λ•Œλ¬Έμ— 비ꡐ λŒ€μƒμ€ μ‹œμž‘μ μ˜ μƒ‰μœΌλ‘œ ν•˜μ˜€λ‹€.

 

ν˜„μž¬ μƒ‰μ’…μ΄μ˜ λͺ¨λ“  칸을 ν™•μΈν•˜λ©΄μ„œ λ‹€λ₯Έ 색을 κ°€μ§„ 칸이 μžˆλ‹€λ©΄, 4λ“±λΆ„ν•œ 색쒅이λ₯Ό λ‚˜νƒ€λ‚΄λŠ” μž¬κ·€ν•¨μˆ˜λ₯Ό μ‹€ν–‰ν•œλ‹€.

ν˜„μž¬ μƒ‰μ’…μ΄μ˜ λͺ¨λ“  칸을 ν™•μΈν•˜λ©΄μ„œ λ‹€λ₯Έ 색을 κ°€μ§„ 칸이 μ—†λ‹€λ©΄, μ‹œμž‘μ μ˜ 색과 λͺ¨λ‘ κ°™λ‹€λŠ” μ˜λ―Έμ΄λ―€λ‘œ, μ‹œμž‘μ μ˜ 색을 κ°€μ§„ 색쒅이 수λ₯Ό μ¦κ°€μ‹œν‚¨λ‹€. 0은 ν•˜μ–€μƒ‰, 1은 νŒŒλž€μƒ‰μ΄λ‹€.

 

πŸ‘Š 풀이

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;	
int n;
vector<vector<int>> paper(128, vector<int>(128));
vector<int> result(2);

void slice(int start_x, int start_y, int length){
	int color = paper[start_x][start_y];
	for (int i=start_x; i<start_x+length; i++){
		for (int j=start_y; j<start_y+length; j++){
			if (color != paper[i][j]){
				slice(start_x, start_y, length/2);
				slice(start_x+length/2, start_y, length/2);
				slice(start_x, start_y+length/2, length/2);
				slice(start_x+length/2, start_y+length/2, length/2);
				return;
			}
		}
	}
	result[color] += 1;
	return;
}


int main() {
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	
	cin >> n;
	for (int i=0; i<n; i++){
		for (int j=0; j<n; j++){
			cin >> paper[i][j];
		}
	}
	slice(0, 0, n);
	cout << result[0] << "\n" << result[1] << "\n";
	return 0;
}

 

πŸ“ λ©”λͺ¨

1074번 Z와 λΉ„μŠ·ν•œ 문제