結果

問題 No.1169 Row and Column and Diagonal
ユーザー ntk-ta01ntk-ta01
提出日時 2020-08-14 23:18:18
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,186 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 111,456 KB
最終ジャッジ日時 2024-04-18 23:08:46
合計ジャッジ時間 4,280 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
61,904 KB
testcase_01 AC 39 ms
55,580 KB
testcase_02 AC 39 ms
55,584 KB
testcase_03 AC 49 ms
63,076 KB
testcase_04 AC 69 ms
72,380 KB
testcase_05 AC 58 ms
69,276 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    sys.setrecursionlimit(10**6)
    from random import shuffle
    N = int(input())
    ans = [[-1]*N for _ in range(N)]
    for i in range(N):
        ans[i][i] = i+1
    B = [i for i in range(1, N+1)]

    def dfs(i, j, ans):
        if i == j:
            if i == N-1:
                written = True
                return written, ans
            else:
                return dfs(i, j+1, ans)
        canuse = [True]*(N+1)
        for k in range(N):
            if ans[i][k] != -1:
                canuse[ans[i][k]] = False
            if ans[k][j] != -1:
                canuse[ans[k][j]] = False

        written = False
        shuffle(B)
        if any(c for c in canuse):
            for v in B:
                if not written and canuse[v]:
                    ans[i][j] = v
                    if j+1 < N:
                        written, ans = dfs(i, j+1, ans)
                    else:
                        written, ans = dfs(i+1, 0, ans)
            if not written:
                ans[i][j] = -1
        return written, ans

    _, ans = dfs(0, 0, ans)

    for i in range(N):
        print(*ans[i])


if __name__ == '__main__':
    main()
0