結果

問題 No.1169 Row and Column and Diagonal
ユーザー ikdikd
提出日時 2020-08-14 22:44:25
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 1,323 bytes
コンパイル時間 1,880 ms
コンパイル使用メモリ 175,048 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-04-18 22:35:07
合計ジャッジ時間 6,002 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn read<T: std::str::FromStr>() -> T {
    let token: String = std::io::stdin()
        .bytes()
        .map(|c| c.ok().unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

fn solve(a: &mut Vec<Vec<usize>>, i: usize, j: usize) -> bool {
    let n = a.len();
    if i >= n {
        return true;
    }
    let mut next_i = i;
    let mut next_j = j + 1;
    if next_j == n {
        next_i = i + 1;
        next_j = 0;
    }
    if i == j {
        return solve(a, next_i, next_j);
    }
    let mut seen = std::collections::HashSet::new();
    for k in 0..n {
        seen.insert(a[k][j]);
        seen.insert(a[i][k]);
    }
    let xs = (1..=n).filter(|x| !seen.contains(x));
    for x in xs {
        a[i][j] = x;
        if solve(a, next_i, next_j) {
            return true;
        }
        a[i][j] = 0;
    }
    return false;
}

fn main() {
    let n: usize = read();
    let mut a = vec![vec![0; n]; n];
    for i in 0..n {
        a[i][i] = i + 1;
    }
    assert!(solve(&mut a, 0, 0));
    for r in a {
        println!(
            "{}",
            r.iter()
                .map(|x| x.to_string())
                .collect::<Vec<_>>()
                .join(" ")
        );
    }
}
0