fn main() { let stdin = std::io::stdin(); let p = Problem::read(stdin.lock()); let mut ops: Vec = vec![0; p.k]; search(&p.b, p.k, &p.a, &mut ops); let mut ss: Vec> = vec![vec![]; p.k]; let mut a = p.a.clone(); for (i, &op) in ops.iter().enumerate().rev() { if op < a.n { ss[i].extend(a.select_col(op)); a.operate_col(op); } else { ss[i].extend(a.select_row(op - a.n)); a.operate_row(op - a.n); } } let mut ans: Vec = Vec::with_capacity(200); for (&op, s) in ops.iter().zip(ss.iter()) { let mut x = s[op % p.n]; let cnt = cycle(s) - 1; for _ in 0..cnt { if op < p.n { ans.push(x); } else { ans.push(x + p.n); } x = s[x]; } } use std::io::Write; let mut buf: Vec = Vec::with_capacity(2024); writeln!(&mut buf, "{}", ans.len()).unwrap(); for &op in ans.iter() { if op < p.n { writeln!(&mut buf, "C {}", op + 1).unwrap(); } else { writeln!(&mut buf, "R {}", op + 1 - p.n).unwrap(); } } let stdout = std::io::stdout(); stdout.lock().write_all(&buf).unwrap(); } fn search(g: &PPuzzle, k: usize, z: &PPuzzle, v: &mut Vec) -> bool { if k == 0 { return z.eq(g); } let k = k - 1; for &i in g.f[k].iter() { v[k] = i; let mut w = z.clone(); w.operate_col(i); if search(g, k, &w, v) { return true; } v[k] = i + g.n; let mut w = z.clone(); w.operate_row(i); if search(g, k, &w, v) { return true; } } false } fn cycle(v: &[usize]) -> usize { let mut w: Vec = vec![0; v.len()]; let mut t: Vec = v.to_vec(); let mut cnt: usize = 0; loop { for (i, &x) in v.iter().enumerate() { w[x] = t[i]; } cnt += 1; if w == v { return cnt; } t.copy_from_slice(&w); } } #[derive(Clone, Eq)] struct PPuzzle { n: usize, i_col: Vec, i_row: Vec, f: std::rc::Rc>>, } impl PPuzzle { fn at(&self, r: usize, c: usize) -> usize { self.f[self.i_row[r]][self.i_col[c]] } fn select_col(&self, c: usize) -> impl Iterator + '_ { (0..self.n).map(move |r| self.at(r, c)) } fn select_row(&self, r: usize) -> impl Iterator + '_ { (0..self.n).map(move |c| self.at(r, c)) } fn operate_col(&mut self, c: usize) { let s: Vec = self.select_col(c).collect(); let t: Vec = self.i_col.clone(); for (i, x) in s.into_iter().enumerate() { self.i_col[x] = t[i]; } } fn operate_row(&mut self, r: usize) { let s: Vec = self.select_row(r).collect(); let t: Vec = self.i_row.clone(); for (i, x) in s.into_iter().enumerate() { self.i_row[x] = t[i]; } } fn iter(&self) -> impl Iterator + '_ { let n = self.n; (0..n).flat_map(move |r| (0..n).map(move |c| self.at(r, c))) } } impl PartialEq for PPuzzle { fn eq(&self, other: &PPuzzle) -> bool { self.iter().eq(other.iter()) } } impl std::iter::FromIterator for PPuzzle { fn from_iter(iter: T) -> PPuzzle where T: IntoIterator, { let x: Vec = iter.into_iter().map(|v| v - 1).collect(); let n: usize = (x.len() as f64).sqrt().floor() as usize; let f: Vec> = x.chunks(n).map(ToOwned::to_owned).collect(); PPuzzle { n, i_col: (0..n).collect(), i_row: (0..n).collect(), f: std::rc::Rc::new(f), } } } struct Problem { n: usize, k: usize, a: PPuzzle, b: PPuzzle, } impl Problem { fn read(reader: R) -> Problem { let mut x: Vec = std::io::read_to_string(reader) .unwrap() .split_ascii_whitespace() .map(str::parse) .filter_map(Result::ok) .collect(); let n: usize = x.drain(..1).next().unwrap(); let k: usize = x.drain(..1).next().unwrap(); let a: PPuzzle = x.drain(..n * n).collect(); let b: PPuzzle = x.into_iter().collect(); Problem { n, k, a, b } } }