You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

1 year ago
fn get_matrix(n: usize) -> u32 {
let mut xss: Vec<Vec<u32>> = Vec::new();
for _i in 0..n {
let mut r = Vec::new();
for _j in 0..n {
r.push(0u32);
}
xss.push(r);
}
xss[n/2][n/2] = 1;
let mvs = [(1, 0), (0, -1), (-1, 0), (0, 1)];
let mut mvi = 0usize;
let mut l = 1;
let mut li = 0;
let mut i: usize = n / 2;
let mut j: usize = n / 2 + 1;
let mut res = 1;
for k in 2u32..= (n * n) as u32 {
xss[i][j] = k;
let mv = mvs[mvi];
//println!("mvi: {}", mvi);
if mv.0 == 1 { i += 1; }
if mv.0 == -1 { i -= 1; }
if mv.1 == 1 { j += 1; }
if mv.1 == -1 { j-= 1; }
li += 1;
//println!("li vs l: {} vs {}", li, l);
if li == l {
mvi += 1;
li = 0;
if mvi == 1 { l += 1; }
if mvi == 3 { l += 1; }
if mvi == 4 { mvi = 0; }
let d = if mvi == 0 { k } else { k + 1 };
//println!("{}", d);
res += d;
}
}
//for i in 0..n {
// for j in 0..n {
// print!("{}\t", xss[i][j]);
// }
// println!("");
//}
res
}
fn main() {
println!("{}", get_matrix(1001));
}