n = $n; $pp->icol = array_combine(range(1, $n), range(1, $n)); $pp->irow = array_combine(range(1, $n), range(1, $n)); $pp->matrix = array_fill(1, $n, []); for ($r = 1; $r <= $n; $r++) { $s = explode(" ", fgets(STDIN)); $pp->matrix[$r] = array_fill(1, $n, 0); for ($c = 1; $c <= $n; $c++) { $pp->matrix[$r][$c] = intval(array_shift($s)); } } return $pp; } public function size() { return $this->n; } public function at($r, $c) { return $this->matrix[$this->irow[$r]][$this->icol[$c]]; } public function selectCol($x) { $v = array_fill(1, $this->n, 0); for ($r = 1; $r <= $this->n; $r++) { $v[$r] = $this->at($r, $x); } return $v; } public function selectRow($y) { $v = array_fill(1, $this->n, 0); for ($c = 1; $c <= $this->n; $c++) { $v[$c] = $this->at($y, $c); } return $v; } public function operateCol($x) { $p = $this->selectCol($x); $t = array_fill(1, $this->n, 0); for ($i = 1; $i <= $this->n; $i++) { $t[$p[$i]] = $this->icol[$i]; } $this->icol = $t; } public function operateRow($y) { $p = $this->selectRow($y); $t = array_fill(1, $this->n, 0); for ($i = 1; $i <= $this->n; $i++) { $t[$p[$i]] = $this->irow[$i]; } $this->irow = $t; } public function isSame($other) { for ($r = 1; $r <= $this->n; $r++) { for ($c = 1; $c <= $this->n; $c++) { if ($this->at($r,$c) !== $other->at($r,$c)) { return false; } } } return true; } } function search($b, $k, $t, &$v) { if ($k === 0) { return $t->isSame($b); } for ($i = 1; $i <= $b->size()*2; $i++) { $v[] = $i; $s = clone $t; if ($i <= $b->size()) { $s->operateCol($i); } else { $s->operateRow($i-$b->size()); } if (search($b,$k-1,$s,$v)) { return true; } array_pop($v); } return false; } function cycle($p) { $t = array_fill(1, count($p), 0); $w = array_slice($p, 0, null, true); for ($cnt = 1;; $cnt++) { for ($i = 1; $i <= count($p); $i++) { $t[$p[$i]] = $w[$i]; } if ($p == $t) { return $cnt; } list($t, $w) = [$w, $t]; } } function solve($n, $k, $a, $b) { $ops = []; search($b, $k, $a, $ops); $ans = []; $t = clone $a; foreach ($ops as $op) { if ($op <= $n) { $p = $t->selectCol($op); $t->operateCol($op); } else { $p = $t->selectRow($op-$n); $t->operateRow($op-$n); } $z = $op <= $n ? $op : ($op-$n); $tmp = []; for ($cnt = cycle($p)-1; $cnt > 0; $cnt--) { $z = $p[$z]; $tmp[] = $op <= $n ? $z : ($z+$n); } $ans = array_merge($tmp, $ans); } return $ans; } fscanf(STDIN, "%d%d", $n, $k); $a = PPuzzle::read($n); $b = PPuzzle::read($n); $ans = solve($n, $k, $a, $b); echo count($ans), PHP_EOL; foreach ($ans as $op) { if ($op <= $n) { echo "C ", $op, PHP_EOL; } else { echo "R ", ($op-$n), PHP_EOL; } }