VOOZH about

URL: https://www.geeksforgeeks.org/dsa/designing-algorithm-to-solve-ball-sort-puzzle/

⇱ Designing algorithm to solve Ball Sort Puzzle - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Designing algorithm to solve Ball Sort Puzzle

Last Updated : 3 Dec, 2024

In Ball Sort Puzzle game, we have p balls of each colour and n different colours, for a total of pƗn balls, arranged in n stacks. In addition, we have 2 empty stacks. A maximum of p balls can be in any stack at a given time. The goal of the game is to sort the balls by colour in each of the n stacks.

Rules:

  • Only the top ball of each stack can be moved.
  • A ball can be moved on top of another ball of the same colour
  • A ball can be moved in an empty stack.

Refer to the following GIF for an example game play (Level-7):


šŸ‘ Ball-Sort-Puzzle
Ball Sort Puzzle


Approach I [Recursion and Backtrack]:

  • From the given rules, a simple recursive algorithm could be generated as below:
    • Start with the given initial position of all the balls
    • Create an initial empty Queue.
    • loop:
      • If the current position is sorted:
        • return
      • else
        • Enqueue all possible moves in a Queue.
        • Dequeue the next move from the Queue.
        • Go to loop.

However, the approach looks simple and correct, it has few caveats:

  • Incorrect:
    • We might end up in an infinite loop if there are >1 moves in the Queue which lead to the same position of balls.
  • Inefficient:
    • We might end up visiting the same position multiple times.

Thus, eliminating the above-mentioned bottlenecks would solve the issue.

Approach II [Memoization using HashMap]:

  • Assumptions:
    • We'll represent ball positions as a vector of strings: {"gbbb", "ybry", "yggy", "rrrg"}
  • Create a set called Visited of <String> which will contain the visited positions as one long string.
  • Create an empty vector for Answer which will store positions<a, b> of the tubes to move the top ball from tube a to and put it in tube b.
  • Initialise grid with the initial settings of the balls.
  • func solver(grid):
    • add grid to Visited
    • loop over all the stacks (i):
      • loop over all the stacks (j):
        • If move i->j is valid, create newGrid with that move.
          • if the balls are sorted in newGrid,
            • update Answer;
            • return;
          • if newGrid is NOT in Visited
            • solver(newGrid)
            • if solved:
              • update Answer

Sample Game Input I:

šŸ‘ Image
Level 3

Sample Input I: 

5
ybrb
byrr
rbyy

Sample Output I:

Move 1 to 4 1 times
Move 1 to 5 1 times
Move 1 to 4 1 times
Move 2 to 5 2 times
Move 1 to 2 1 times
Move 3 to 1 1 times
Move 1 to 2 1 times
Move 3 to 1 1 times
Move 2 to 1 3 times
Move 2 to 3 1 times
Move 3 to 4 1 times
Move 3 to 2 1 times
Move 2 to 4 1 times
Move 3 to 5 1 times

Sample Game Input II:

šŸ‘ Image
Level 5

Sample Input II:

6
gbbb
ybry
yggy
rrrg

Sample Output II:

Move 1 to 5 3 times
Move 2 to 6 1 times
Move 3 to 6 1 times
Move 1 to 3 1 times
Move 2 to 1 1 times
Move 2 to 5 1 times
Move 2 to 6 1 times
Move 3 to 2 3 times
Move 3 to 6 1 times
Move 4 to 2 1 times
Move 1 to 4 1 times

Below is the implementation:


Output
Move 1 to 5 3 times
Move 2 to 6 1 times
Move 3 to 6 1 times
Move 1 to 3 1 times
Move 2 to 1 1 times
Move 2 to 5 1 times
Move 2 to 6 1 times
Move 3 to 2 3 times
Move 3 to 6 1 times
Move 4 to 2 1 times
...

Time Complexity: O(n!) where n is the number of stacks.
Auxiliary Space: O(n^2)

Comment