Note that there are some explanatory texts on larger screens.

plurals
  1. POTraversing through all possible moves
    primarykey
    data
    text
    <p>Ok I am sort of stuck with this problem.</p> <p>I have a Puzzle object that has an array for amounts { 1, 2, 3} and an array representing the target {3, 3, 0} and a move function that takes two integers i &amp; j and applies it to the array. </p> <p>Now I am trying to write a solver for this. So the idea is that we apply all possible values for i &amp; j and keep applying it until we get a desired target.</p> <p>I am trying to use a recursive method to handle this but what I am not able to grasp is how to stem my recursion into different ways and find the best "set of moves" that gets me to the target.</p> <p>So the list of possible moves:</p> <p>(0, 1), (0, 2), (1, 0), (1, 2), (2, 1), (2, 0)</p> <p>After each of these move we get a new "amounts array" and we need to apply these moves onto that and check if any of them results in the "target array".</p> <p>So my pseudocode is:</p> <pre><code>solve(puzzle): if puzzle.isSolved: return true; else: solve(puzzle.move(0, 1)); solve(puzzle.move(0, 2)); solve(puzzle.move(1, 0)); solve(puzzle.move(1, 2)); solve(puzzle.move(2, 0)); solve(puzzle.move(2, 1)); </code></pre> <ul> <li>We are assuming that puzzle.move function returns the puzzle with new state.</li> </ul> <p>Now I am sure I am doing something horribly wrong here.. but I can't seem to put my finger on it. So any thoughts / hints / pointers would be appreciated.</p> <p>Thanks.</p> <p><strong>Edit</strong></p> <p>Ok so couple of things to make this more clear for everyone:</p> <p>Since puzzle.move returns the puzzle after applying the move, I am thinking of creating a new puzzle so that the recursion goes through that. Basically, what needs to happen is that after each move we will have a new state of amount array.</p> <p>So after a move (i = 0, j = 1) the puzzle with initial amount of (a, b, c) has an amounts (a-a, b+a, c). We than take this new amount array and apply the next move (i = 0, j = 2) and so one.</p> <p>But that is just one part of the "tree" there is another path that needs to be checked which is when we apply the move (i = 0, j = 2) on the initial amount and then go on from there.</p> <p>Hope this helps :)</p> <p>btw, this is problem is known as Water Jug Problem (http://www.cut-the-knot.org/ctk/Water.shtml)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload