풀이
체스판은 인접한 칸의 색이 다르므로, 64개의 칸 중 어느 하나라도 색이 결정되면 나머지 칸의 색도 모두 결정됩니다.
코드
def get_inverted_color(color: str) -> str:
return "B" if color == "W" else "W"
def get_repaint_count(row: int, col: int, first_color: str) -> int:
ret = 0
color = first_color
for i in range(row, row + 8):
for j in range(col, col + 8):
if board[i][j] != color:
ret += 1
color = get_inverted_color(color)
color = get_inverted_color(color)
return ret
def main():
global board
N, M = map(int, input().split())
board = [input() for _ in range(N)]
answer = N * M
for i in range(N - 7):
for j in range(M - 7):
answer = min(
answer, get_repaint_count(i, j, "W"), get_repaint_count(i, j, "B")
)
print(answer)
main()