본문 바로가기
Algorithm/C++

[백준 1780] 종이의 개수

by imagineer_jinny 2022. 9. 8.

1780번: 종이의 개수 (acmicpc.net)

 

1780번: 종이의 개수

N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1 중 하나가 저장되어 있다. 우리는 이 행렬을 다음과 같은 규칙에 따라 적절한 크기로 자르려고 한다. 만약 종이가 모두 같은 수

www.acmicpc.net

 

한국어를 이해 못하다가 이거 보고 이해함

[백준] 1780번 - 종이의 개수 | ChanBLOG (chanhuiseok.github.io)

 

 

정답 코드

// Authored by : cpprhtn
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/b8488e82105d49e89ca6f39fd8ee665b
#include <bits/stdc++.h>
using namespace std;

int N;
int paper[2200][2200];
int cnt[3]; //-1, 0, 1로 채워진 종이 갯수

//해당 종이 내부에 같은 숫자로만 채워졌는지 확인하는 함수
bool check(int x, int y, int n) {
  for (int i = x; i < x + n; i++)
  for (int j = y; j < y + n; j++)
    if (paper[x][y] != paper[i][j])
    return false;
  return true;
}
void solve(int x, int y, int z)
{
  if (check(x, y, z)) {
    cnt[paper[x][y] + 1] += 1;
    return;
  }
  int n = z / 3;
  for (int i = 0; i < 3; i++)
  for (int j = 0; j < 3; j++)
    solve(x + i * n, y + j * n, n);
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++)
  for (int j = 0; j < N; j++)
    cin >> paper[i][j];
  solve(0, 0, N);
  for (int i = 0; i < 3; i++) cout << cnt[i] << "\n";
}

'Algorithm > C++' 카테고리의 다른 글

[백준 1992] 쿼드트리  (0) 2022.09.10
[백준 2630] 색종이 만들기  (0) 2022.09.08
[백준 17478] 재귀함수가 뭔가요?  (0) 2022.09.07
[백준 1074] Z  (0) 2022.09.07
[백준 1629] 곱셈  (0) 2022.09.06

댓글