| 5 | import java.util.Random; |
| 6 | |
| 7 | public class Floppy { |
| 8 | private static byte[][] distance = new byte[24][16]; |
| 9 | private static String[] turn = {"U", "R", "D", "L"}; |
| 10 | private static int[] seq = new int[10]; |
| 11 | static int[] arr = new int[4]; |
| 12 | |
| 13 | static { |
| 14 | for (int i = 0; i < 24; i++) |
| 15 | for (int j = 0; j < 16; j++) |
| 16 | distance[i][j] = -1; |
| 17 | distance[0][0] = 0; |
| 18 | int n = 1; |
| 19 | for (int depth = 0; depth < 8; depth++) { |
| 20 | //n = 0; |
| 21 | for (int i = 0; i < 24; i++) |
| 22 | for (int j = 0; j < 16; j++) |
| 23 | if (distance[i][j] == depth) |
| 24 | for (int k = 0; k < 4; k++) { |
| 25 | int cp = permMove(i, k); |
| 26 | int eo = flipMove(j, k); |
| 27 | if (distance[cp][eo] == -1) { |
| 28 | distance[cp][eo] = (byte) (depth + 1); |
| 29 | n++; |
| 30 | } |
| 31 | } |
| 32 | Log.w("dct", depth + 1 + "\t" + n); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | private static int permMove(int cp, int m) { |
| 37 | Utils.idxToPerm(arr, cp, 4, false); |
| 38 | switch (m) { |
| 39 | case 0: Utils.swap(arr, 0, 1); break; //U2 |
| 40 | case 1: Utils.swap(arr, 1, 2); break; //R2 |
| 41 | case 2: Utils.swap(arr, 2, 3); break; //D2 |
| 42 | case 3: Utils.swap(arr, 0, 3); break; //L2 |
| 43 | } |
| 44 | return Utils.permToIdx(arr, 4, false); |
| 45 | } |
| 46 | |
| 47 | private static int flipMove(int eo, int m) { |
| 48 | Utils.idxToFlip(arr, eo, 4, false); |
| 49 | arr[m] = 1 - arr[m]; |
| 50 | return Utils.flipToIdx(arr, 4, false); |
| 51 | } |
| 52 | |
| 53 | private static boolean search(int cp, int eo, int d, int lm) { |
| 54 | if (d == 0) return cp == 0 && eo == 0; |
| 55 | if (distance[cp][eo] > d) return false; |
| 56 | for (int i = 0; i < 4; i++) { |
| 57 | if (i != lm) { |
| 58 | int cpi = permMove(cp, i); |
| 59 | int eoi = flipMove(eo, i); |
| 60 | if (search(cpi, eoi, d - 1, i)) { |
| 61 | seq[d] = i; |
| 62 | //sb.insert(0, turn[i] + " "); |
| 63 | return true; |
| 64 | } |