import scala.io.StdIn.readLine type Matrix = Array[Array[Int]] def reorderColsByColPerm(s: Matrix, x: Int) = val n = s.length val t = s.map(_.clone).toArray for r <- 0 until n do for c <- 0 until n do s( r )( t(c)(x) ) = t(r)(c) def reorderRowsByRowPerm(s: Matrix, y: Int) = val n = s.length val t = s.map(_.clone).toArray for r <- 0 until n do for c <- 0 until n do s( t(y)(r) )( c ) = t(r)(c) def selectCol(s: Matrix, x: Int): Array[Int] = s.map(_(x)).toArray def selectRow(s: Matrix, y: Int): Array[Int] = s(y).clone def isSame(a: Matrix, b: Matrix): Boolean = a.corresponds(b)(_.sameElements(_)) def search(b: Matrix, k: Int, t: Matrix, ops: Array[Int]): Boolean = if k == 0 then return isSame(b, t) else val n = b.length for op <- 0 until 2*n do ops(ops.length-k) = op val s = t.map(_.clone).toArray if op < n then reorderColsByColPerm(s,op) else reorderRowsByRowPerm(s,op-n) if search(b, k-1, s, ops) then return true return false def solve(n: Int, k: Int, a: Matrix, b: Matrix): List[Int] = val ops = new Array[Int](k) search(b, k, a, ops) var ans = List[Int]() for op <- ops do val p = if op < n then selectCol(a,op) else selectRow(a,op-n) var z = op % n var tmp = List[Int]() var t = (0 until n).toArray var w = p.clone while !p.sameElements(t) do z = p(z) tmp = (if op < n then z else (z+n)) :: tmp for i <- 0 until n do t(p(i)) = w(i) w = t.clone ans = tmp.tail.reverse ::: ans if op < n then reorderColsByColPerm(a,op) else reorderRowsByRowPerm(a,op-n) return ans def readInts() = readLine().split(" ").map(_.toInt-1) @main def main() = val Array(n,k) = readInts().map(_+1) val a: Matrix = (1 to n).map(_ => readInts()).toArray val b: Matrix = (1 to n).map(_ => readInts()).toArray val ans = solve(n,k,a,b) println(ans.length) for op <- ans do if op < n then println("C " + (op+1)) else println("R " + (op+1-n))