반응형

[백준] 1018번 체스판 다시 칠하기 - python

 

https://www.acmicpc.net/problem/1018

 

1018번: 체스판 다시 칠하기

첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.

www.acmicpc.net

예제 입력 1 복사

8 8
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW

예제 출력 1 복사

1

예제 입력 2 복사

10 13
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
WWWWWWWWWWBWB
WWWWWWWWWWBWB

예제 출력 2 복사

12

예제 입력 3 복사

8 8
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB

예제 출력 3 복사

0

예제 입력 4 복사

9 23
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBW

예제 출력 4 복사

31

예제 입력 5 복사

10 10
BBBBBBBBBB
BBWBWBWBWB
BWBWBWBWBB
BBWBWBWBWB
BWBWBWBWBB
BBWBWBWBWB
BWBWBWBWBB
BBWBWBWBWB
BWBWBWBWBB
BBBBBBBBBB

예제 출력 5 복사

0

예제 입력 6 복사

8 8
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWWWB
BWBWBWBW

예제 출력 6 복사

2

예제 입력 7 복사

11 12
BWWBWWBWWBWW
BWWBWBBWWBWW
WBWWBWBBWWBW
BWWBWBBWWBWW
WBWWBWBBWWBW
BWWBWBBWWBWW
WBWWBWBBWWBW
BWWBWBWWWBWW
WBWWBWBBWWBW
BWWBWBBWWBWW
WBWWBWBBWWBW

예제 출력 7 복사

15
import sys

N, M = map(int, sys.stdin.readline().split())

board = []
white_first = []
black_first = []

for _ in range(N):
    row = sys.stdin.readline().replace("\n", "")
    board.append([i for i in row])

initial_color = board[0][0]

# 흰색으로 시작하는 체스판을 만들 경우
for index, row in enumerate(board):
    painting = []
    if index % 2 == 0: current_color = "W"
    else: current_color = "B"

    for value in row:
        if value == current_color: painting.append(0)
        else: painting.append(1)
        
        if current_color == "W": current_color = "B"
        else: current_color = "W"
    white_first.append(painting)

# 검은색으로 시작하는 체스판을 만들 경우
for index, row in enumerate(board):
    painting = []
    if index % 2 == 0: current_color = "B"
    else: current_color = "W"

    for value in row:
        if value == current_color: painting.append(0)
        else: painting.append(1)
        
        if current_color == "W": current_color = "B"
        else: current_color = "W"
    black_first.append(painting)

# 최솟값을 초기화 할 때, 보드의 최대 크기인 50*50 = 2500으로 한다.
min_count = 2500
for i in range(N-8+1):
    rows = white_first[i:i+8]
    for j in range(M-8+1):
        paint = 0
        for row in rows:
            paint += sum(row[j:j+8])
        if paint < min_count: min_count = paint

for i in range(N-8+1):
    rows = black_first[i:i+8]
    for j in range(M-8+1):
        paint = 0
        for row in rows:
            paint += sum(row[j:j+8])
        if paint < min_count: min_count = paint

print(min_count)

# 참조 : https://nerogarret.tistory.com/35

반응형

+ Recent posts