Files
2024-08-29 16:22:27 +09:00

38 lines
1.3 KiB
Rust

use std::io::{self, Write, BufWriter};
fn main(){
let mut n = String::new();
io::stdin().read_line(&mut n)
.expect("Failed to read line");
let n = n.trim().parse::<u32>().expect("Not an integer");
let stdout = io::stdout();
let lock = stdout.lock();
let mut buff = BufWriter::new(lock);
let mut check : Vec<Option<String>> = (0..100*n+20).map(|x| None).collect();
buff.write_fmt(format_args!("{}\n", 2_u32.pow(n) - 1)).expect("Failed to write");
let res = move_plate(n, 1, 3, &mut check);
buff.write(res.unwrap().as_bytes()).expect("Failed to write");
}
fn move_plate(n: u32, from: u8, to: u8, check: &mut [Option<String>]) -> Option<String>{
let hash : usize = 100 * (n as usize) + 10 * (from as usize) + (to as usize);
if let Some(res) = &check[hash]{
return Some(res.clone());
}
if n == 1{
let res : String = format_args!("{} {}\n", from, to).to_string();
check[hash] = Some(res.clone());
return Some(res);
}
let remain : u8 = 6 - from - to;
let res1 = move_plate(n - 1, from, remain, check).unwrap();
let res2 = move_plate(n - 1, remain, to, check).unwrap();
let res = Some(format_args!("{}{} {}\n{}", res1, from, to, res2).to_string());
check[hash] = res.clone();
return res;
}