2
$\begingroup$

Thanks to your help to solve previous problems, I have been able to advance in the learning of mathematica. It seems that this project to make a solver for the 8-puzzle has surpassed my abilities in mathematica, since I have not been able to do a function that calculates the amount of linear conflicts given a board, it is very important to keep in mind that it should not be Count the zero to make this calculation. Searching on the web to reach this web page, where the explanation given by the author helped me understand that it is a linear conflict, here is a screenshot, so that you also know what it is.

part one

part two

Here is what I have been able to do, very much to my dismay, if it takes zero into account to say how many conflicts there are on a board.

sameRow[i_, j_] := Quotient[i-1, 3] == Quotient[j-1, 3];
 adjacentColumn[i_, j_] := Abs[i-j] == 1;
 sameColumn[i_, j_] := Mod[i-1, 3] == Mod[j-1, 3];
 adjacentRow[i_, j_] := Abs[i-j] == 3;
 directos[board_] := Module[{v = Flatten[board]/.0->9},
    Total[Table[If[v[[v[[i]]]] == i &&
         (sameRow[i, v[[i]]] && adjacentColumn[i, v[[i]]] ||
          sameColumn[i, v[[i]]] && adjacentRow[i, v[[i]]]), 1, 0], {i, 1, 9}]]/2];

Testit with the board above, we have

directos [{[4,2,5},{1,0,6},{3,8,7}}]

But if we try it with the next one, we'll notice that it takes zero into account, which should not be.

directos[{{1,2,3},{4,5,0},{7,8,6}}]

Do you know any way to achieve this goal? It is important to advance in the construction of the solver I am planning to do. Thanks in advance for your help, without which I can not move forward.

$\endgroup$
2
  • 1
    $\begingroup$ some typos to fix: Module[{v = Flatten[board] /. 0 -> 9}, Total[....]] $\endgroup$
    – kglr
    Commented Jul 17, 2018 at 23:04
  • $\begingroup$ @kglr Thank you for your observation, and modify them according to your suggestion $\endgroup$ Commented Jul 18, 2018 at 1:05

1 Answer 1

1
$\begingroup$

Add the condition that i and v[[i]] cannot be 9 (the value of the blank tile after the replacement 0 -> 9):

ClearAll[directos]
directos[board_] := Module[{v = Flatten[board] /. 0 -> 9}, 
   Total[Table[Boole[v[[i]] != 9 &&  i != 9 && 
    (v[[v[[i]]]] == i && (sameRow[i, v[[i]]] && adjacentColumn[i, v[[i]]] || 
    sameColumn[i, v[[i]]] && adjacentRow[i, v[[i]]]))], {i, 1, 9}]] / 2];

The examples in OP:

directos[{{4, 2, 5}, {1, 0, 6}, {3, 8, 7}}]

1

directos[{{1, 2, 3}, {4, 5, 0}, {7, 8, 6}}]

0

$\endgroup$
2
  • $\begingroup$ Thank you very much for your answer, I expected more answers to know what improvements I could make, I'm going ahead to know more about MMA. $\endgroup$ Commented Jul 19, 2018 at 19:02
  • $\begingroup$ @bullitohappy, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Commented Jul 19, 2018 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.