結果

問題 No.460 裏表ちわーわ
ユーザー h_nosonh_noson
提出日時 2017-01-26 23:38:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,266 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 10,792 KB
実行使用メモリ 8,256 KB
最終ジャッジ日時 2023-08-25 01:42:48
合計ジャッジ時間 13,656 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,072 KB
testcase_01 WA -
testcase_02 AC 58 ms
8,144 KB
testcase_03 AC 16 ms
8,032 KB
testcase_04 AC 16 ms
8,048 KB
testcase_05 AC 27 ms
8,036 KB
testcase_06 AC 27 ms
8,040 KB
testcase_07 AC 16 ms
8,188 KB
testcase_08 AC 16 ms
8,052 KB
testcase_09 AC 21 ms
7,988 KB
testcase_10 AC 329 ms
8,112 KB
testcase_11 AC 27 ms
8,052 KB
testcase_12 AC 163 ms
8,100 KB
testcase_13 AC 1,169 ms
7,968 KB
testcase_14 AC 1,292 ms
8,000 KB
testcase_15 AC 1,300 ms
8,016 KB
testcase_16 AC 27 ms
8,036 KB
testcase_17 AC 1,271 ms
8,060 KB
testcase_18 AC 1,288 ms
8,200 KB
testcase_19 AC 1,307 ms
8,080 KB
testcase_20 AC 26 ms
8,144 KB
testcase_21 AC 1,125 ms
8,192 KB
testcase_22 AC 1,260 ms
8,156 KB
testcase_23 AC 1,274 ms
8,044 KB
testcase_24 AC 16 ms
8,104 KB
testcase_25 AC 15 ms
8,152 KB
testcase_26 AC 16 ms
8,048 KB
testcase_27 AC 18 ms
8,084 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):
                if a[y-1][j]:
                    return INF
        return dfs(y+1,0)
    elif y == m:
        for i in range(m):
            for j in range(n):
                if a[i][j]:
                    return INF
        return 0
    cy = y - (y > 0)
    ok = True
    for j in range(n):
        if a[cy][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