Notice
Recent Posts
Recent Comments
Link
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

DANIELOGIC

[Python] 백준 1018번 체스판 다시 칠하기 본문

Algorithm/백준

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

daniel; 2024. 12. 24. 06:34

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

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

문제

코드

n, m = map(int, input().split())
l = []
for i in range(n):
    l.append(list(input()))

w_first = []
b_first = []
for i in range(n):
    temp_list1 = []
    temp_list2 = []
    for j in range(m):
        if (j%2 == 0 and i%2 == 0) or (j%2 != 0 and i%2 != 0):
            temp_list1.append("W")
            temp_list2.append("B")
        else:
            temp_list1.append("B")
            temp_list2.append("W")
    w_first.append(temp_list1)
    b_first.append(temp_list2)

a, b = 0, 0
count_list = []
while True:
    if b+8 > m:
        b = 0
        a += 1
        if a+8 > n:
            break
    w_count = 0
    b_count = 0    
    for i in range(a,a+8):
        for j in range(b,b+8):
            if l[i][j] == w_first[i][j]:
                w_count += 1
            if l[i][j] == b_first[i][j]:
                b_count += 1
    count_list.append(min(w_count,b_count))
    b += 1

if len(count_list) == 0:
    print(0)
else:
    print(min(count_list))

리뷰

처음에는 첫 원소를 기준으로 인접한 원소들을 하나씩 계산하는 방법을 선택했었다.

그렇게 되면 복잡한 연산 과정을 두 번이나 거쳐야 했으므로 비효율적이라고 느꼈다.

첫 원소가 W, B인 두 가지만 가능하므로

각 경우에 대한 배열을 미리 생성하고

이를 비교해서 count_list에 저장하는 방법을 선택했다.

다른 사람이 짠 코드와 비교했을 때, 내 코드는 직관적으로 이해하기 쉬웠다. 

단점은 코드 길이가 길다.

 

오랜만에 백준 푸니까 재밌다.

이제 종강해서 자주 올릴 예정이다.