[python] 백준 1018번

도덩이의 개발 일지·2024년 9월 4일

백준

목록 보기
65/131
post-thumbnail

안녕하세요 !

오늘은 백준 - 체스판 다시 칠하기 문제를 가지고 왔습니다.


문제 설명


해결 방법

문제를 해결한 방법을 간략히 적어보겠습니다.

  1. 입력 받는다.
  2. 맨 위쪽 위 칸이 검은색과 흰색 중 하나로 된 8x8 크기의 체스판을 각각 만든다.
  3. 입력받은 체스판에서 만들 수 있는 8x8 크기의 체스판의 맨 위쪽 위칸의 좌표를 구하고 좌표를 함수의 인자로 넘긴다.
  4. 검은색과 흰색 중 하나로 된 8x8 크기의 체스판과 입력받은 체스판을 8x8 크기로 잘라낸 체스판의 색이 다른 것을 카운트 한다.
  5. 색이 다른 것을 카운트한 것들 가운데 가장 작은 수를 구한다.

  1. 입력 받는다.
import sys
num = list(map(int, sys.stdin.readline().strip().split()))
map_1 = []
for i in range(num[0]):
    map_1.append(list(sys.stdin.readline().strip()))

  1. 맨 위쪽 위 칸이 검은색과 흰색 중 하나로 된 8x8 크기의 체스판을 각각 만든다.
map_compare_1 = []
map_compare_2 = []
for y in range(8):
    m_1 = []
    m_2 = []
    for x in range(8):
        if y%2 and x%2:
            m_1.append('W')
            m_2.append('B')
        elif y%2 and not x%2:
            m_1.append('B')
            m_2.append('W')
        elif not y%2 and x%2:
            m_1.append('B')
            m_2.append('W')
        elif not y%2 and not x%2:
            m_1.append('W')
            m_2.append('B')
    map_compare_1.append(m_1)
    map_compare_2.append(m_2)

  1. 입력받은 체스판에서 만들 수 있는 8x8 크기의 체스판의 맨 위쪽 위칸의 좌표를 구하고 좌표를 함수의 인자로 넘긴다.
count = []
for y in range(num[0] - 7):
    for x in range(num[1] - 7):
        return_cnt = compare(x, y, map_1)
        count += return_cnt

  1. 검은색과 흰색 중 하나로 된 8x8 크기의 체스판과 입력받은 체스판을 8x8 크기로 잘라낸 체스판의 색이 다른 것을 카운트 한다.
def compare(x, y, map_1):
    cnt = [0,0]
    for i in range(8):
        for j in range(8):
            if map_1[i+y][j+x] != map_compare_1[i][j]:
                cnt[0] += 1
            elif map_1[i+y][j+x] != map_compare_2[i][j]:
                cnt[1] += 1
    return cnt

  1. 색이 다른 것을 카운트한 것들 가운데 가장 작은 수를 구한다.
print(min(count))

전체 코드

import sys
num = list(map(int, sys.stdin.readline().strip().split()))

map_1 = []
for i in range(num[0]):
    map_1.append(list(sys.stdin.readline().strip()))

map_compare_1 = []
map_compare_2 = []
for y in range(8):
    m_1 = []
    m_2 = []
    for x in range(8):
        if y%2 and x%2:
            m_1.append('W')
            m_2.append('B')
        elif y%2 and not x%2:
            m_1.append('B')
            m_2.append('W')
        elif not y%2 and x%2:
            m_1.append('B')
            m_2.append('W')
        elif not y%2 and not x%2:
            m_1.append('W')
            m_2.append('B')
    map_compare_1.append(m_1)
    map_compare_2.append(m_2)

def compare(x, y):
    cnt = [0,0]
    for i in range(8):
        for j in range(8):
            if map_1[i+y][j+x] != map_compare_1[i][j]:
                cnt[0] += 1
            elif map_1[i+y][j+x] != map_compare_2[i][j]:
                cnt[1] += 1
    return cnt

count = []
for y in range(num[0] - 7):
    for x in range(num[1] - 7):
        return_cnt = compare(x, y, map_1)
        count += return_cnt

print(min(count))
profile
말하는 감자에서 개발자로 ( ´͈ ᵕ `͈ )◞♡

0개의 댓글