백준 1080 행렬 문제 간단 풀이

less than 1 minute read

n,m = map(int,input().split())

a = []
b = []
for i in range(n):
    a.append(list(map(int, input())))

for j in range(n):
    b.append(list(map(int, input())))

cnt = 0
answer = 1
for i in range(n-2):    
    for j in range(m-2):
        if a[i][j] != b[i][j]: #a,b가 다를 경우
            cnt += 1
            for k in range(i, i+3): #3x3 바꿔주기
                for l in range(j,j+3):
                    a[k][l] = 1 - a[k][l]

        else:
            continue
            
for i in range(n):
    if a[i] != b[i]:
        print(-1)
        answer = -1
        break

if answer == 1:
    print(cnt)


Leave a comment