프로그래밍 알고리즘

[정올 1335] 색종이만들기(영역구분)

꾸준한사람 2023. 1. 3. 06:53
반응형

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=614&sca=99&sfl=wr_hit&stx=1335 

 

JUNGOL

 

www.jungol.co.kr

#include <stdio.h>

enum { WHITE = 0, BLUE, CMAX };
int N, M[130][130], Ccnt[CMAX];

void Count(int sr, int sc, int er, int ec) {
	int color = M[sr][sc], pass = 1;
	for (int i = sr; i <= er; i++) {
		for (int j = sc; j <= ec; j++) {
			if (M[i][j] != color) {
				pass = 0; break;
			}
		}
		if (pass == 0) break;
	}
	int mr = (sr + er) / 2, mc = (sc + ec) / 2;
	
	if (pass) Ccnt[color]++;
	else {
		Count(sr, sc, mr, mc); //왼위
		Count(mr + 1, sc, er, mc); //왼아래
		Count(sr, mc + 1, mr, ec); //오위
		Count(mr + 1, mc + 1, er, ec); //오아래
	}
}

int main(void) {
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) scanf("%d", &M[i][j]);
	Count(1, 1, N, N);
	printf("%d\n%d\n", Ccnt[WHITE], Ccnt[BLUE]);

	return 0;
}
반응형