結果

問題 No.460 裏表ちわーわ
ユーザー h_nosonh_noson
提出日時 2017-01-26 23:49:04
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 1,292 bytes
コンパイル時間 356 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 79,940 KB
最終ジャッジ日時 2023-08-25 01:43:09
合計ジャッジ時間 6,324 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
77,568 KB
testcase_01 AC 76 ms
76,204 KB
testcase_02 AC 134 ms
78,432 KB
testcase_03 AC 69 ms
71,588 KB
testcase_04 AC 69 ms
71,432 KB
testcase_05 AC 110 ms
77,716 KB
testcase_06 AC 117 ms
77,732 KB
testcase_07 AC 140 ms
78,044 KB
testcase_08 AC 165 ms
78,820 KB
testcase_09 AC 99 ms
77,568 KB
testcase_10 AC 171 ms
78,636 KB
testcase_11 AC 109 ms
77,676 KB
testcase_12 AC 130 ms
78,252 KB
testcase_13 AC 245 ms
79,680 KB
testcase_14 AC 267 ms
78,932 KB
testcase_15 AC 297 ms
79,188 KB
testcase_16 AC 106 ms
77,676 KB
testcase_17 AC 273 ms
79,940 KB
testcase_18 AC 240 ms
79,048 KB
testcase_19 AC 251 ms
79,228 KB
testcase_20 AC 107 ms
77,692 KB
testcase_21 AC 264 ms
79,140 KB
testcase_22 AC 270 ms
79,356 KB
testcase_23 AC 240 ms
79,544 KB
testcase_24 AC 296 ms
79,844 KB
testcase_25 AC 63 ms
71,476 KB
testcase_26 AC 73 ms
76,460 KB
testcase_27 AC 81 ms
76,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 10**9
dx = [-1,-1,-1, 0, 1, 1, 1, 0]
dy = [-1, 0, 1, 1, 1, 0,-1,-1]

def dfs(y,x):
    if x == n:
        if y > 0:
            for j in range(n-2,n):
                if a[y-1][j]:
                    return INF
        return dfs(y+1,0)
    elif y == m:
        for i in range(m-2,m):
            for j in range(n):
                if a[i][j]:
                    return INF
        return 0
    if y > 0:
        ok = True
        for j in range(n):
            if a[y-1][j]:
                ok = False
        if ok:
            return dfs(y+1,0)
    ret = INF
    if x == 0 or y == 0 or a[y-1][x-1]:
        a[y][x] ^= 1
        for i in range(8):
            ny = y + dy[i]
            nx = x + dx[i]
            if ny < 0 or ny >= m or nx < 0 or nx >= n: continue
            a[ny][nx] ^= 1
        ret = min(ret,dfs(y,x+1)+1)
        a[y][x] ^= 1
        for i in range(8):
            ny = y + dy[i]
            nx = x + dx[i]
            if ny < 0 or ny >= m or nx < 0 or nx >= n: continue
            a[ny][nx] ^= 1
    if x == 0 or y == 0 or not a[y-1][x-1]:
        ret = min(ret,dfs(y,x+1))
    return ret

m,n = map(int,input().split(' '))
a = [list(map(int,input().split(' '))) for i in range(m)]

ans = dfs(0,0)
if ans < INF:
    print(ans)
else:
    print("Impossible")
0