Exercise2
This commit is contained in:
39
Exercise/Homework2/Q3.rs
Normal file
39
Exercise/Homework2/Q3.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::io;
|
||||
|
||||
pub struct Status{
|
||||
pos: i32,
|
||||
a: char, // from
|
||||
b: char, // to
|
||||
c: char // aux
|
||||
}
|
||||
|
||||
|
||||
fn main(){
|
||||
let mut s = String::new();
|
||||
io::stdin().read_line(&mut s).unwrap();
|
||||
let n: i32 = s.trim().parse().unwrap();
|
||||
|
||||
let mut stack: Vec<Status> = Vec::new();
|
||||
stack.push(Status{pos: n, a: 'A', b: 'C', c: 'B'});
|
||||
let mut max = 0;
|
||||
|
||||
let mut output = String::new(); //输出,要不然太慢总会超时
|
||||
|
||||
while !stack.is_empty() {
|
||||
if stack.len() > max {
|
||||
max = stack.len();
|
||||
}
|
||||
|
||||
let mut end = stack.pop().unwrap();
|
||||
if end.pos == 1 {
|
||||
output.push_str(&format!("Move disk from {} to {}\n", end.a, end.b));
|
||||
} else {
|
||||
stack.push(Status{pos: end.pos - 1, a: end.c, b: end.b, c: end.a});
|
||||
stack.push(Status{pos: 1, a: end.a, b: end.b, c: end.c});
|
||||
stack.push(Status{pos: end.pos - 1, a: end.a, b: end.c, c: end.b});
|
||||
|
||||
}
|
||||
}
|
||||
print!("{}", output);
|
||||
println!("{}", max);
|
||||
}
|
||||
Reference in New Issue
Block a user