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

2023. 10. 20. 22:00·PS/백준 알고리즘[BOJ]
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

'PS > 백준 알고리즘[BOJ]' 카테고리의 다른 글

[백준 1780번] 종이의 개수 (C++)  (0) 2023.10.21
[백준 1992번] 쿼드트리 (C++)  (0) 2023.10.21
[백준 1074번] Z (C++)  (0) 2023.10.18
[백준 10799번] 쇠막대기 (C++)  (0) 2023.10.12
[백준 9012번] 괄호 (C++)  (0) 2023.10.12
'PS/백준 알고리즘[BOJ]' 카테고리의 다른 글
  • [백준 1780번] 종이의 개수 (C++)
  • [백준 1992번] 쿼드트리 (C++)
  • [백준 1074번] Z (C++)
  • [백준 10799번] 쇠막대기 (C++)
BE_개발자
BE_개발자
경이로운 BE 개발자가 되기 위한 프로그래밍 공부 기록장
    250x250
  • BE_개발자
    경이로운 개발일기
    BE_개발자
  • 전체
    오늘
    어제
    • 전체 보기 (213)
      • AI (1)
        • AI native (0)
        • Skill (0)
      • SpringBoot (4)
        • JPA (3)
        • Security (0)
        • 튜토리얼 (1)
        • 기타 (0)
      • Infra (0)
        • Docker (0)
        • AWS (0)
        • NCP (0)
        • GCP (0)
      • React (19)
      • 서버 (0)
      • Computer Science (16)
        • SW Engineering (10)
        • Data Base (2)
        • OS(운영 체제) (4)
      • Data science (0)
        • Probability & Random Variab.. (0)
        • Data Analysis(데이터 분석) (0)
      • 자료구조 | 알고리즘 (57)
        • 선형 자료구죠 (6)
        • 비선형 자료구조 (9)
        • 정렬(Sort) (3)
        • 탐색(Brute Force) (7)
        • 분할 정복(Devide Conquer) (3)
        • 동적 계획법 (6)
        • 탐욕(Greedy) (2)
        • 수학 (7)
        • 심화 알고리즘 (10)
      • PS (44)
        • 백준 알고리즘[BOJ] (38)
        • 프로그래머스[programmers] (3)
      • Dev tool (15)
        • 개발 도구 및 환경 (0)
        • vscode (3)
        • Git Hub (4)
        • Chrome 웹스토어 (5)
        • Python 전용 개발환경 (0)
      • 성장기록 (1)
      • 개인 project (14)
        • 홈페이지 만들기 (11)
        • 냉보미 (1)
      • STL(Standard Library) (9)
      • programming Language (1)
        • javascript (1)
      • 기타 (13)
        • html css (11)
      • (책, 글, 블로그)리뷰 (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 백준
    • Github
    • Notion
    • 서울과학기술대학교 학술동아리 EC
  • 공지사항

  • 인기 글

  • 태그

    OS
    순열
    완전 탐색
    재귀함수
    PS
    BFS
    괄호쌍
    운영체제
    stl
    탐색
    알고리즘
    자료구조
    수학
    분할정복
    브루트포스
    프론트엔드
    이분 탐색
    백트래킹
    스택
    C++
    비트마스킹
    스프링부트
    DP
    react
    stack
    SW 공학
    그리디
    SW Engineering
    CS
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
BE_개발자
[백준 2630번] 색종이 만들기 (C++)
상단으로

티스토리툴바