fork download
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4.  
  5. class Field {
  6. final boolean[] field;
  7. final int x, y;
  8. final int hop;
  9.  
  10. Field(int x, int y) {
  11. field = new boolean[9];
  12. activate(x, y);
  13. this.x = x;
  14. this.y = y;
  15. this.hop = 0;
  16. }
  17.  
  18. Field(Field field, int tx, int ty) {
  19. this.field = Arrays.copyOf(field.field, 9);
  20. activate(tx, ty);
  21. this.x = tx;
  22. this.y = ty;
  23. this.hop = field.hop + 1;
  24. }
  25.  
  26. private void activate(int x, int y) {
  27. if (isActive(x, y))
  28. throw new AssertionError("Active cell");
  29.  
  30. field[x + 3 * y] = true;
  31. }
  32.  
  33. private boolean isActive(int x, int y) {
  34. return field[x + 3 * y];
  35. }
  36.  
  37. boolean isConnectable(int x0, int y0, int x1, int y1) {
  38. if (!isActive(x0, y0))
  39. throw new AssertionError("Non-active cell");
  40.  
  41. if (isActive(x1, y1))
  42. return false;
  43.  
  44. int dx = Math.abs(x0 - x1);
  45. int dy = Math.abs(y0 - y1);
  46.  
  47. if (x1 == x0 && dy == 2 && !isActive(x1, 1))
  48. return false;
  49.  
  50. if (y1 == y0 && dx == 2 && !isActive(1, y1))
  51. return false;
  52.  
  53. if (dx == dy && dx == 2 && !isActive(1, 1))
  54. return false;
  55.  
  56. return true;
  57. }
  58. }
  59.  
  60. public class Main {
  61. public static void main(String[] args) {
  62. int idx = 0;
  63. List<Field> a = new ArrayList<Field>();
  64.  
  65. for (int x = 0; x < 3; x++) {
  66. for (int y = 0; y < 3; y++) {
  67. a.add(new Field(x, y));
  68. }
  69. }
  70.  
  71. while (idx < a.size()) {
  72. Field currentField = a.get(idx);
  73. nextStep(a, currentField);
  74. idx++;
  75. }
  76.  
  77. int c = 0;
  78. for (Field field : a)
  79. if (field.hop >= 4)
  80. c++;
  81.  
  82. System.out.println(c);
  83. }
  84.  
  85. public static void nextStep(List<Field> a, Field field) {
  86. for (int x = 0; x < 3; x++) {
  87. for (int y = 0; y < 3; y++) {
  88. if (field.isConnectable(field.x, field.y, x, y)) {
  89. a.add(new Field(field, x, y));
  90. }
  91. }
  92. }
  93. }
  94. }
  95.  
Success #stdin #stdout 0.3s 380160KB
stdin
Standard input is empty
stdout
387488