목차

동전 뒤집기

ps
링크acmicpc.net/…
출처BOJ
문제 번호1640
문제명동전 뒤집기
레벨골드 4
분류

애드혹

시간복잡도O(n+m)
인풋사이즈n<=1000, m<=1000
사용한 언어Python 3.11
제출기록31256KB / 40ms
최고기록40ms
해결날짜2023/07/24

풀이

코드

"""Solution code for "BOJ 1640. 동전 뒤집기".

- Problem link: https://www.acmicpc.net/problem/1640
- Solution link: http://www.teferi.net/ps/problems/boj/1640

Tags: [ad hoc]
"""

import sys


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    grid = [sys.stdin.readline().rstrip() for _ in range(N)]

    odd_row_count = sum(row.count('1') % 2 for row in grid)
    odd_col_count = sum(col.count('1') % 2 for col in zip(*grid))

    if (odd_row_count + odd_col_count) % 2 == 1:
        print('-1')
        return

    if odd_row_count % 2 == 0:
        answer = odd_row_count + odd_col_count
    else:
        answer = odd_row_count + M - odd_col_count
    answer = min(answer, M + N - answer)

    print(answer)


if __name__ == '__main__':
    main()