結果
問題 | No.2986 Permutation Puzzle |
ユーザー | ID 21712 |
提出日時 | 2024-12-18 01:45:35 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 708 ms / 2,000 ms |
コード長 | 2,915 bytes |
コンパイル時間 | 2,705 ms |
コンパイル使用メモリ | 91,764 KB |
実行使用メモリ | 58,804 KB |
最終ジャッジ日時 | 2024-12-18 01:45:57 |
合計ジャッジ時間 | 19,140 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 138 ms
44,960 KB |
testcase_01 | AC | 158 ms
46,872 KB |
testcase_02 | AC | 162 ms
46,624 KB |
testcase_03 | AC | 564 ms
58,804 KB |
testcase_04 | AC | 135 ms
40,420 KB |
testcase_05 | AC | 163 ms
41,800 KB |
testcase_06 | AC | 152 ms
41,868 KB |
testcase_07 | AC | 152 ms
41,672 KB |
testcase_08 | AC | 154 ms
42,040 KB |
testcase_09 | AC | 251 ms
47,760 KB |
testcase_10 | AC | 169 ms
42,108 KB |
testcase_11 | AC | 183 ms
43,096 KB |
testcase_12 | AC | 280 ms
48,824 KB |
testcase_13 | AC | 154 ms
41,724 KB |
testcase_14 | AC | 210 ms
44,000 KB |
testcase_15 | AC | 254 ms
48,100 KB |
testcase_16 | AC | 155 ms
41,460 KB |
testcase_17 | AC | 215 ms
44,088 KB |
testcase_18 | AC | 343 ms
48,844 KB |
testcase_19 | AC | 186 ms
42,516 KB |
testcase_20 | AC | 257 ms
48,304 KB |
testcase_21 | AC | 220 ms
46,376 KB |
testcase_22 | AC | 163 ms
41,760 KB |
testcase_23 | AC | 281 ms
48,844 KB |
testcase_24 | AC | 708 ms
54,488 KB |
testcase_25 | AC | 530 ms
51,748 KB |
testcase_26 | AC | 513 ms
52,472 KB |
testcase_27 | AC | 313 ms
48,680 KB |
testcase_28 | AC | 555 ms
54,688 KB |
testcase_29 | AC | 398 ms
49,256 KB |
testcase_30 | AC | 561 ms
54,584 KB |
testcase_31 | AC | 484 ms
51,712 KB |
testcase_32 | AC | 260 ms
47,128 KB |
testcase_33 | AC | 549 ms
52,876 KB |
testcase_34 | AC | 496 ms
52,692 KB |
testcase_35 | AC | 462 ms
50,532 KB |
testcase_36 | AC | 259 ms
45,804 KB |
testcase_37 | AC | 299 ms
48,536 KB |
testcase_38 | AC | 519 ms
54,312 KB |
testcase_39 | AC | 428 ms
49,636 KB |
testcase_40 | AC | 150 ms
41,180 KB |
testcase_41 | AC | 439 ms
51,452 KB |
testcase_42 | AC | 471 ms
51,840 KB |
testcase_43 | AC | 643 ms
54,504 KB |
ソースコード
import java.util.*; class Main { public static void main(String[] args) throws java.lang.Exception { final Scanner sc = new Scanner(System.in); final int n = sc.nextInt(); final int k = sc.nextInt(); final int[][] a = new int[n][]; for (int r = 0; r < n; r++) { a[r] = new int[n]; for (int c = 0; c < n; c++) { a[r][c] = sc.nextInt() - 1; } } final int[][] b = new int[n][]; for (int r = 0; r < n; r++) { b[r] = new int[n]; for (int c = 0; c < n; c++) { b[r][c] = sc.nextInt() - 1; } } final int[] ans = solve(n, k, a, b); System.out.println(ans.length); for (int op : ans) { if (op < n) { System.out.println("C " + (op+1)); } else { System.out.println("R " + (op+1-n)); } } } static int[] solve(final int n, final int k, final int[][] a, final int[][] b) { int[] ops = new int[k]; search(b, k, a, ops); List<Integer> ans = new ArrayList<>(); int[][] t = deepcopy(a); for (int i = 0; i < k; i++) { final int op = ops[k-1-i]; final int[] v = op < n ? selectCol(t, op) : selectRow(t, op-n); if (op < n) { reorderColsByColPerm(t, op); } else { reorderRowsByRowPerm(t, op-n); } final List<Integer> tmp = new ArrayList<>(); for (int cnt = cycle(v) - 1, z = op % n; cnt > 0; cnt--) { z = v[z]; tmp.add(op < n ? z : (z+n)); } ans.addAll(0, tmp); } return ans.stream().mapToInt(Integer::intValue).toArray(); } static void reorderColsByColPerm(final int[][] s, final int x) { int[][] t = deepcopy(s); for (int r = 0; r < s.length; r++) { for (int c = 0; c < s.length; c++) { s[ r ][ t[c][x] ] = t[r][c]; } } } static void reorderRowsByRowPerm(final int[][] s, final int y) { int[][] t = deepcopy(s); for (int r = 0; r < s.length; r++) { for (int c = 0; c < s.length; c++) { s[ t[y][r] ][ c ] = t[r][c]; } } } static int[] selectCol(final int[][] s, final int x) { return Arrays.stream(s).mapToInt(row -> row[x]).toArray(); } static int[] selectRow(final int[][] s, final int y) { return s[y].clone(); } static boolean search(final int[][] b, final int k, final int[][] t, final int[] v) { if (k == 0) { return Arrays.deepEquals(b, t); } for (int i = 0; i < b.length*2; i++) { v[k-1] = i; int[][] s = deepcopy(t); if (i < b.length) { reorderColsByColPerm(s, i); } else { reorderRowsByRowPerm(s, i - b.length); } if (search(b, k-1, s, v)) { return true; } } return false; } static int cycle(final int[] v) { int[] t = new int[v.length]; int[] w = v.clone(); for (int cnt = 1; ; cnt++) { for (int i = 0; i < v.length; i++) { t[ v[i] ] = w[i]; } if (Arrays.equals(t, v)) { return cnt; } int[] tmp = t; t = w; w = tmp; } } static int[][] deepcopy(final int[][] s) { return Arrays.stream(s).map(int[]::clone).toArray(int[][]::new); } }