fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut maze: Vec> = vec![vec![0; n]; n]; let mut x: usize = 0; let mut y: usize = 0; for i in 1..=n*n { maze[y][x] = i; let at_top = (y == 0 && x < n-1) || (y > 0 && x < n-1 && maze[y-1][x] > 0 && maze[y][x+1] == 0); let at_right = !at_top && ((x == n-1 && y < n-1) || (x < n-1 && y < n-1 && maze[y][x+1] > 0 && maze[y+1][x] == 0)); let at_bottom = !at_top && !at_right && ((y == n-1 && x > 0) || (y < n-1 && x > 0 && maze[y+1][x] > 0 && maze[y][x-1] == 0)); let at_left = !at_top && !at_right && !at_bottom && ((x == 0 && y > 1) || (x > 0 && y > 0 && maze[y][x-1] > 0 && maze[y-1][x] == 0)); if at_top { x += 1; } else if at_right { y += 1; } else if at_bottom { x -= 1; } else if at_left { y -= 1; } } maze.iter().for_each(|line| { println!("{}", line.iter().map(|i| format!("{:03}", i)).collect::>().join(" ")); }) }