結果
問題 | No.2986 Permutation Puzzle |
ユーザー | ID 21712 |
提出日時 | 2024-12-18 01:40:34 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,911 bytes |
コンパイル時間 | 2,968 ms |
コンパイル使用メモリ | 92,548 KB |
実行使用メモリ | 54,708 KB |
最終ジャッジ日時 | 2024-12-18 01:40:55 |
合計ジャッジ時間 | 19,628 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 182 ms
42,020 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 170 ms
42,240 KB |
testcase_09 | WA | - |
testcase_10 | AC | 179 ms
41,900 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | AC | 175 ms
42,220 KB |
testcase_41 | AC | 521 ms
51,384 KB |
testcase_42 | AC | 505 ms
51,776 KB |
testcase_43 | AC | 662 ms
54,632 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[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); } }