Guest User

Untitled

a guest
Jan 15th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. 1 4
  2. 0 2 5 7
  3. 3 6
  4.  
  5. 5 3
  6. 2 8 1 7
  7. 6 4
  8.  
  9. 2 6
  10. 0 3 7 10
  11. 1 4 8 11
  12. 5 9
  13.  
  14. include "globals.mzn";
  15.  
  16. int: rows;
  17. int: cols;
  18. int: n = sum(i in 1..rows, j in 1..cols) ( bool2int(mat[i,j] > 0) );
  19. array[1..rows, 1..cols] of int: mat;
  20.  
  21. array[1..n] of var 1..n: x; % decision variables
  22.  
  23. solve satisfy;
  24.  
  25. constraint
  26. all_different(x)
  27. /
  28. % constrain all the neighbours for each cell
  29. forall(i in 1..rows, j in 1..cols where mat[i,j] > 0) (
  30. forall(a,b in {-1,0,1} where
  31. i+a >= 1 / j+b >= 1 /
  32. i+a <= rows / j+b <= cols /
  33. not(a = 0 / b = 0) /
  34. mat[i+a,j+b] > 0
  35. )
  36. ( % the constraints
  37. abs(x[mat[i,j]] - x[mat[i+a,j+b]])>1
  38. )
  39. )
  40. ;
  41.  
  42. output [ show(x) ];
  43.  
  44. % Problem 2: 12 sized
  45. rows = 4;
  46. cols = 4;
  47.  
  48. % 0 means empty cell
  49. mat = array2d(1..rows, 1..cols,
  50. [
  51. 0, 3, 7, 0,
  52. 1, 4, 8, 11,
  53. 2, 5, 9, 12,
  54. 0, 6, 10, 0
  55. ]);
  56.  
  57. import java.util.*;
  58.  
  59. public class NumberCrossPuzzle
  60. {
  61. public static void main(String[] args)
  62. {
  63. int w = 4, h = 4, m = 1, n = 1;
  64.  
  65. Map<State, Long> freq = new HashMap<State, Long>();
  66. freq.put(State.initialState(w, h, m, n), 1L);
  67.  
  68. int squares = w * h - 4 * m * n;
  69. for (int i = 0; i < squares; i++) {
  70. Map<State, Long> nextFreq = new HashMap<State, Long>();
  71. for (Map.Entry<State, Long> e : freq.entrySet()) {
  72. for (State succ : e.getKey().successors(w, h)) {
  73. Long l = nextFreq.get(succ);
  74. if (l == null) l = 0L;
  75. if (l < 0L) throw new IllegalStateException("Overflow detected");
  76. nextFreq.put(succ, l + e.getValue());
  77. }
  78. }
  79. freq = nextFreq;
  80. }
  81.  
  82. Long total = 0L;
  83. for (Long l : freq.values()) {
  84. if (l < 0L) throw new IllegalStateException("Overflow detected");
  85. total += l;
  86. if (total < 0L) throw new IllegalStateException("Overflow detected");
  87. }
  88. System.out.println(total);
  89. }
  90.  
  91. private static class State
  92. {
  93. private final int mask, x, y;
  94.  
  95. private State(int mask, int x, int y) {
  96. this.mask = mask;
  97. this.x = x;
  98. this.y = y;
  99. }
  100.  
  101. static State initialState(int w, int h, int m, int n) {
  102. if (w * h > 32) throw new IllegalArgumentException("Need a larger mask");
  103. if (m * 2 >= w) throw new IllegalArgumentException("Corner holes too wide");
  104. if (n * 2 >= h) throw new IllegalArgumentException("Corner holes too high");
  105.  
  106. int mask = 0;
  107. for (int y = 0; y < n; y++) {
  108. for (int x = 0; x < m; x++) {
  109. // TL, TR, BL, BR
  110. mask ^= 1 << (y * w + x);
  111. mask ^= 1 << ((w - 1 - y) * w + x);
  112. mask ^= 1 << (y * w + (h - 1 - x));
  113. mask ^= 1 << ((w - 1 - y) * w + (h - 1 - x));
  114. }
  115. }
  116.  
  117. return new State(mask, -1, -1);
  118. }
  119.  
  120. Iterable<State> successors(int w, int h) {
  121. List<State> rv = new ArrayList<State>();
  122. for (int j = 0; j < h; j++) {
  123. for (int i = 0; i < w; i++) {
  124. int bit = 1 << (j * w + i);
  125. if ((mask & bit) != 0) continue;
  126. if ((x - i) * (x - i) + (y - j) * (y - j) <= 2) continue;
  127. rv.add(new State(mask | bit, i, j));
  128. }
  129. }
  130. return rv;
  131. }
  132.  
  133. @Override
  134. public int hashCode() {
  135. return (mask * 37 + x) * 37 + y;
  136. }
  137.  
  138. @Override
  139. public boolean equals(Object obj) {
  140. if (!(obj instanceof State)) return false;
  141. State s = (State)obj;
  142. return s.mask == mask && s.x == x && s.y == y;
  143. }
  144. }
  145. }
Add Comment
Please, Sign In to add comment