Advertisement
DEMOOH

Untitled

Dec 4th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. In[240]:= n = 3;
  2. m = 6;
  3.  
  4. In[242]:=
  5. GetValue[i_, j_] :=
  6. If[i == 0 || j == 0 || i == n + 1 || j == m + 1, 0, matrix[[i, j]]];
  7. GetNeighbours[i_, j_] :=
  8. GetValue[i - 1, j] + GetValue[i, j - 1] + GetValue[i + 1, j] +
  9. GetValue[i, j + 1];
  10.  
  11. In[244]:=
  12. recur[hashMatrix_] := Block[{i, j, hash = hashMatrix, isEnd = 1},
  13. For[i = 1, i <= n, ++i,
  14. For[j = 1, j <= m, ++j,
  15. If[matrix[[i, j]] != 1 && GetNeighbours[i, j] == 1,
  16. isEnd = 0;
  17. matrix[[i, j]] = 1;
  18. hash = BitSet[hash, i*m + j - (m + 1)];
  19. If[mHash[[hash]] == 0,
  20. recur[hash];
  21. ];
  22. matrix[[i, j]] = 0;
  23. hash = BitClear[hash, i*m + j - (m + 1)];
  24. ];
  25. ];
  26. ];
  27. If[isEnd == 1,(*If[count<100,Print[matrix//TableForm]];*)++count];
  28. mHash[[hash]] = 1;
  29. ];
  30.  
  31. In[245]:= matrix = Table[0, {i, 1, 3}, {j, 1, 6}];
  32. mHash = Table[0, 2^(m*n)];
  33. count = 0;
  34. hash = 0;
  35. TimeConstrained[For[i = 1, i <= n, ++i,
  36. For[j = 1, j <= m, ++j,
  37. matrix[[i, j]] = 1;
  38. hash = BitSet[hash, i*m + j - (m + 1)];
  39. recur[hash];
  40. matrix[[i, j]] = 0;
  41. hash = BitClear[hash, i*m + j - (m + 1)];
  42. ];
  43. ];
  44. , 30];
  45. count
  46.  
  47. Out[250]= 452
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement