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.
87 lines
1.6 KiB
87 lines
1.6 KiB
struct Step { |
|
i: i8, |
|
j: i8 |
|
} |
|
|
|
struct Steps { |
|
l: usize, |
|
i: usize, |
|
j: usize, |
|
k: usize, |
|
s: usize |
|
} |
|
|
|
impl Steps { |
|
fn new(n: usize) -> Steps { |
|
return Steps { |
|
i: n / 2, |
|
j: n / 2, |
|
l: 1, |
|
k: 1, |
|
s: 0, |
|
}; |
|
} |
|
|
|
fn ads(&mut self, s: &Step) { |
|
if s.i < 0 { self.i -= 1; } |
|
if s.i > 0 { self.i += 1; } |
|
if s.j < 0 { self.j -= 1; } |
|
if s.j > 0 { self.j += 1; } |
|
} |
|
|
|
fn next(&mut self) { |
|
let mut sts = Vec::new(); |
|
sts.push(Step { i: 0, j: 1 }); |
|
sts.push(Step { i: -1, j: 0 }); |
|
sts.push(Step { i: 0, j: -1 }); |
|
sts.push(Step { i: 1, j: 0 }); |
|
|
|
if self.k > self.l { |
|
self.k = 1; |
|
self.s += 1; |
|
if self.s == sts.len() { |
|
self.s = 0; |
|
self.l += 1; |
|
} |
|
if self.s == 2 { |
|
self.l += 1; |
|
} |
|
} |
|
|
|
let st = &sts[self.s]; |
|
self.k += 1; |
|
self.ads(st) |
|
} |
|
|
|
} |
|
|
|
fn print() { |
|
let n = 7usize; |
|
let mut m = Vec::<Vec<u32>>::new(); |
|
for i in 0..n { |
|
let mut l = Vec::<u32>::new(); |
|
for j in 0..n { |
|
l.push(0); |
|
} |
|
m.push(l); |
|
} |
|
|
|
let mut st = Steps::new(n); |
|
for i in 1..20 { |
|
m[st.i][st.j] = i; |
|
st.next(); |
|
} |
|
|
|
for i in 0..n { |
|
let l = &m[i]; |
|
for j in 0..n { |
|
print!("{}\t", l[j]); |
|
} |
|
println!(""); |
|
} |
|
} |
|
|
|
fn main() { |
|
println!("hello world"); |
|
print(); |
|
}
|
|
|