[BOJ] 2630번: 색종이 만들기 (python)

한서영·2023년 4월 11일
0

BOJ

목록 보기
12/15

문제 링크 : https://www.acmicpc.net/problem/2630

💡 해결 방법

  • 좌표도 함수 매개변수에 넣어주기
  • 처음에는 배열을 매개변수로 넣어주고 white일 때와 blue일 때를 모두 고려해서 count 증가하는 방식으로 하려 했음
  • 그냥 체크하려는 정사각형의 가장 왼쪽 위 색이랑 모두 동일하면 되니까 왼쪽 위 칸을 기준으로 하여 이중 for문으로 비교하여 총 white와 blue 개수를 증가시키기

🖥️ 코드 - (미완성)

import sys
from collections import Counter

N = int(sys.stdin.readline())
arr = []
white = 0   #0
blue = 0    #1

for i in range(N):
    arr.append(list(map(int, sys.stdin.readline().rstrip().split(" "))))

def chk(x, y, arr):
    global white
    global blue
    checkWhite = 0
    checkBlue = 0
    check = True
    for i in range(len(arr)):
        count = Counter(arr[i])
        if count['1'] == len(arr):
            checkBlue += 1
        elif count['0'] == len(arr):
            checkWhite += 1
        else:
            check = False
            #나눠야함(재귀)
            break
    if check:
        if checkWhite == len(arr):
            white += 1
        elif checkBlue == len(arr):
            blue += 1
    else:
        chk(x, y, )

chk(0, 0, arr)
print(white)
print(blue)

🖥️ 코드

import sys

N = int(sys.stdin.readline())
arr = []
white = 0   #0
blue = 0    #1

for i in range(N):
    arr.append(list(map(int, sys.stdin.readline().rstrip().split(" "))))

def chk(x, y, N):
    global white
    global blue
    color = arr[x][y]
    for i in range(x, x+N):
        for j in range(y, y+N):
            if color != arr[i][j]:
                chk(x, y, N//2)
                chk(x+N//2, y, N//2)
                chk(x, y+N//2, N//2)
                chk(x+N//2, y+N//2, N//2)
                return            
    if color == 0:
        white += 1
    else:
        blue += 1

chk(0, 0, N)
print(white)
print(blue)

✏️ 알고리즘 분류

  • 분할 정복
  • 재귀

0개의 댓글