use std::io::Read; fn read() -> 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>, 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::>() .join(" ") ); } }