ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1080๋ฒˆ: ํ–‰๋ ฌ

์ฒซ์งธ ์ค„์— ํ–‰๋ ฌ์˜ ํฌ๊ธฐ N M์ด ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ํ–‰๋ ฌ A๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค์Œ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ํ–‰๋ ฌ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ํ–‰๋ ฌ์ด ์ฃผ์–ด ์งˆ ๋•Œ, 3 * 3 ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ฐ˜์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ํ–‰๋ ฌ A, B๊ฐ€ ๊ฐ™์•„์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ตœ์†Œ ์—ฐ์‚ฐ ๊ฐ’์„ ๊ตฌํ•˜๊ณ  ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ˆ์ œ ์ž…๋ ฅ 1์˜ ๊ฒฝ์šฐ ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 2๋ฒˆ์˜ ์‹œ๋„๋กœ B์™€ ๊ฐ™๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

 

 ์—ฌ๊ธฐ์„œ ์ฃผ์˜ ํ• ์ ์ด ์žˆ๋‹ค๋ฉด, ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•˜๋Š” ์œ„์น˜๊ฐ€ B์™€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋งŒ ๋ฐ˜์ „์‹œํ‚ค๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ๋ฐ˜์ „์‹œํ‚ค์ง€ ์•Š์•„ B์™€ ์œ ์‚ฌํ•œ ํ˜•ํƒœ๋กœ ๋‚˜์•„๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


def conversion(x, y):
    for i in range(x, x + 3):
        for j in range(y, y + 3):
            a[i][j] = 1 - a[i][j]


if __name__ == "__main__":
    answer = 0
    n, m = map(int, stdin.readline().split())
    a = [list(map(int, stdin.readline().strip())) for _ in range(n)]
    b = [list(map(int, stdin.readline().strip())) for _ in range(n)]

    for i in range(n - 2):
        for j in range(m - 2):
            if a[i][j] != b[i][j]:
                conversion(i, j)
                answer += 1

    print(answer if a == b else -1)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€