백준 1080번 행렬

highway92·2021년 10월 13일
0

백준

목록 보기
24/27

문제출처 : https://www.acmicpc.net/problem/1080

풀이과정

  1. a,b의 값이 다를 경우 True인 check matrix를 만들어 준다.

  2. y-3,x-3까지의 범위를 살피며 True일 경우 값을 바꿔준다.
    (그 이외의 범위는 index error가 날 수 있으며 누를 수 없기 때문이다.)

  3. 과정이 끝난후 check matrix에 True 값이 남아있다면 -1을 return
    아니면 cnt 를 return 한다.

import sys

def solve():
    input = sys.stdin.readline

    y,x = map(int,input().split())

    a = [input()[:-1] for i in range(y)]
    b = [input()[:-1] for i in range(y)]

    if y<3 or x<3:
        if a != b:
            return print(-1)
        else:
            return print(0)

    check = [[] for i in range(y)]
    cnt = 0

    for i in range(y):
        for j in range(x):
            if a[i][j] == b[i][j]:
                check[i].append(False)
            else:
                check[i].append(True)

    for i in range(y-2):
        for j in range(x-2):
            if check[i][j] == True:
                cnt+= 1
                for _y in range(3):
                    for _x in range(3):
                        if check[i+_y][j+_x] == True:
                            check[i+_y][j+_x] = False
                        else:
                            check[i+_y][j+_x] = True
    for i in range(y):
            for j in range(x):
                if check[i][j] == True:
                    return print(-1)
    
    return print(cnt)


    
solve()

    
profile
웹 개발자로 활동하고 있습니다.

0개의 댓글