// yukicoder - ProblemID 11672 - writer's solution #include #include using namespace std; using matrix = vector>; void reorder_cols_by_col_perm(matrix &s, int x) { matrix t = s; for (int r = 0; r < s.size(); r++) { for (int c = 0; c < s.size(); c++) { s[ r ][ t[c][x] ] = t[r][c]; } } } void reorder_rows_by_row_perm(matrix &s, int y) { matrix t = s; for (int r = 0; r < s.size(); r++) { for (int c = 0; c < s.size(); c++) { s[ t[y][r] ][ c ] = t[r][c]; } } } vector select_col(matrix &s, int x) { vector v; for (int r = 0; r < s.size(); r++) { v.push_back(s[r][x]); } return v; } vector select_row(matrix &s, int y) { return s[y]; } bool search(matrix &b, int k, matrix &s, vector &v) { if (k == 0) { return b == s; } for (int i = 0; i < b.size()*2; i++) { v.push_back(i); matrix t = s; if (i < b.size()) { reorder_cols_by_col_perm(t, i); } else { reorder_rows_by_row_perm(t, i-b.size()); } if (search(b, k-1, t, v)) { return true; } v.pop_back(); } return false; } int cycle(vector &s) { vector t(s.size()); vector w = s; for (int cnt = 1; ; cnt++) { for (int i = 0; i < s.size(); i++) { t[s[i]] = w[i]; } if (s == t) { return cnt; } w = t; } } vector solve(int n, int k, matrix &a, matrix &b) { vector ops; search(b, k, a, ops); vector ans; matrix s = a; for (auto op : ops) { vector perm; int z; if (op < n) { perm = select_col(s, op); reorder_cols_by_col_perm(s, op); z = op; } else { perm = select_row(s, op-n); reorder_rows_by_row_perm(s, op-n); z = op - n; } z = perm[z]; vector tmp; for (int cnt = cycle(perm) - 1; cnt > 0; cnt--) { if (op < n) { tmp.push_back(z); } else { tmp.push_back(z+n); } z = perm[z]; } ans.insert(ans.begin(), tmp.begin(), tmp.end()); } return ans; } int main() { int n, k; matrix a, b; cin >> n >> k; a.resize(n); for (int r = 0; r < n; r++) { a[r].resize(n); for (int c = 0; c < n; c++) { cin >> a[r][c]; a[r][c]--; } } b.resize(n); for (int r = 0; r < n; r++) { b[r].resize(n); for (int c = 0; c < n; c++) { cin >> b[r][c]; b[r][c]--; } } auto ans = solve(n, k, a, b); cout << ans.size() << '\n'; for (auto op : ans) { if (op < n) { cout << "C " << (op+1) << '\n'; } else { cout << "R " << (op+1-n) << '\n'; } } return 0; }