๐Ÿ‘Š 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์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ