백준 1080번

DARTZ·2023년 4월 9일
0

알고리즘

목록 보기
110/135
import sys

N, M = map(int, sys.stdin.readline().split())
matrixBefore = []
matrixAfter = []

for _ in range(N):
    matrixBefore.append(list(map(int, sys.stdin.readline().rstrip())))

for _ in range(N):
    matrixAfter.append(list(map(int, sys.stdin.readline().rstrip())))

def reverse(matrix, row, column):
    for r in range(row, row + 3):
        for c in range(column, column + 3):
            matrix[r][c] = ~matrix[r][c] + 2

    return matrix


def solution(matrixBefore, matrixAfter):

    count = 0

    for row in range(N - 2):
        for column in range(M - 2):
            if matrixBefore[row][column] != matrixAfter[row][column]:
                count += 1
                matrixBefore = reverse(matrixBefore, row, column)

        if matrixBefore[row] != matrixAfter[row]:
            return -1

    if matrixBefore != matrixAfter:
        return -1

    return count

print(solution(matrixBefore, matrixAfter))

원래는 N이나 M이 3이하일 때 -1을 출력하는 코드를 작성했다. 하지만 애초에 두 행렬이 같을 경우 정답이 0이 된다.

반례 참고

1 1
1
1

profile
사람들이 비용을 지불하고 사용할 만큼 가치를 주는 서비스를 만들고 싶습니다.

0개의 댓글