반응형
[백준] 1018번 체스판 다시 칠하기 - python
https://www.acmicpc.net/problem/1018
예제 입력 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
반응형
'프로그래밍 > Baekjoon' 카테고리의 다른 글
[백준] 1021번 회전하는 큐 - PYTHON (0) | 2023.04.12 |
---|---|
[백준] 1019번 책 페이지 - PYTHON (0) | 2023.04.05 |
[백준] 1017번 소수 쌍 - PYTHON (0) | 2023.03.23 |
[백준] 2016번 제곱ㄴㄴ수 - PYTHON (0) | 2023.03.22 |
[백준] 1015번 수열 정렬 - PYTHON (0) | 2023.03.22 |