| 171 | } |
| 172 | |
| 173 | private static boolean search(int p, int t, int l, int lm, int[] seq) { |
| 174 | //searches for solution, from position p|t, in l moves exactly. last move was lm, current depth=d |
| 175 | if (l == 0) return p == 0 && t == 0; |
| 176 | if (perm[p] > l || twst[t] > l) return false; |
| 177 | if (lm == -2) { |
| 178 | int n = r.nextInt(9); |
| 179 | int m = n / 3; |
| 180 | n %= 3; |
| 181 | int q = p, s = t; |
| 182 | for (int a = 0; a <= n; a++) { |
| 183 | q = permmv[q][m]; |
| 184 | s = twstmv[s][m]; |
| 185 | } |
| 186 | if (search(q, s, l - 1, m, seq)) { |
| 187 | seq[l] = m * 3 + n; |
| 188 | return true; |
| 189 | } |
| 190 | } else for (int m = 0; m < 3; m++) { |
| 191 | if (m != lm) { |
| 192 | int q = p, s = t; |
| 193 | for (int a = 0; a < 3; a++) { |
| 194 | q = permmv[q][m]; |
| 195 | s = twstmv[s][m]; |
| 196 | if (search(q, s, l - 1, m, seq)) { |
| 197 | seq[l] = m * 3 + a; |
| 198 | return true; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | private static String solve(int p, int o) { |
| 207 | int[] seq = new int[12]; |