Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In[240]:= n = 3;
- m = 6;
- In[242]:=
- GetValue[i_, j_] :=
- If[i == 0 || j == 0 || i == n + 1 || j == m + 1, 0, matrix[[i, j]]];
- GetNeighbours[i_, j_] :=
- GetValue[i - 1, j] + GetValue[i, j - 1] + GetValue[i + 1, j] +
- GetValue[i, j + 1];
- In[244]:=
- recur[hashMatrix_] := Block[{i, j, hash = hashMatrix, isEnd = 1},
- For[i = 1, i <= n, ++i,
- For[j = 1, j <= m, ++j,
- If[matrix[[i, j]] != 1 && GetNeighbours[i, j] == 1,
- isEnd = 0;
- matrix[[i, j]] = 1;
- hash = BitSet[hash, i*m + j - (m + 1)];
- If[mHash[[hash]] == 0,
- recur[hash];
- ];
- matrix[[i, j]] = 0;
- hash = BitClear[hash, i*m + j - (m + 1)];
- ];
- ];
- ];
- If[isEnd == 1,(*If[count<100,Print[matrix//TableForm]];*)++count];
- mHash[[hash]] = 1;
- ];
- In[245]:= matrix = Table[0, {i, 1, 3}, {j, 1, 6}];
- mHash = Table[0, 2^(m*n)];
- count = 0;
- hash = 0;
- TimeConstrained[For[i = 1, i <= n, ++i,
- For[j = 1, j <= m, ++j,
- matrix[[i, j]] = 1;
- hash = BitSet[hash, i*m + j - (m + 1)];
- recur[hash];
- matrix[[i, j]] = 0;
- hash = BitClear[hash, i*m + j - (m + 1)];
- ];
- ];
- , 30];
- count
- Out[250]= 452
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement