Files
Data-Structure/Exercise/Homework2/Q3.rs
2025-10-17 13:07:18 +08:00

39 lines
1012 B
Rust

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);
}