3 white and 3 black tiles problem

In the 3 white and 3 black tiles problem, there are 7 tiles, 3 are white and 3 are black and there is a white space in the middle. All the black tiles are placed left and all the white tiles are placed right. Each of the white or black tiles can move to the blank space and they can hope over one or two tiles to reach the blank space.

The Approach

  1. Analyze the state-space of the problem.
  2. Propose a heuristic for this problem to move all the white tiles left to all the white tiles, the position of the blank space is not important.

Initial State

Positions0123456
TilesBBBWWW

Goal State

Positions0123456
TilesWWWBBB
3 white and 3 black tiles problem | Reverse the 3 white and 3 black tiles problem
3 white and 3 black tiles problem – Initial State & Goal State

Assumptions

  • Heuristics for Black Tile = tile-distance to the first white tile to its right
  • Heuristics for White Tile = tile-distance to the first black tile to its left * -1
  • We will run the script as long as we will have a non zero value for a tile.
  • If any black tile does not contain any white tile to its left then that tile will have Zero Heuristics
  • If any white tile does not contain any black tile to its right then that tile will have Zero Heuristics
  • We will have a selector value initially set as White.
  • For each iteration, we will change the value to black and white and so no.
  • Depending on the selector we will choose the colored tile with the highest tile, Tile X.
  • X will be moved to the blank space.
  • If the tile jumps to its next tile the cost:= 1 if hop one tile then cost:= 2 if jumps 2 tiles then cost:= 3.

But if you are passionate about system design and architecture, I would suggest you to read Software Architecture Fundamentals, here you will find in depth insights.

3 White And 3 Black Tiles Problem Sudo Code

Selector:= white   
Foreach black tile b
     do h(b):= tile-distance to the first white tile to its right
Foreach white tile w
     do h(w):= tile-distance to the first black tile to its left * -1
 
While any tile has a non-zero h value
   Do
        If selector:= white then,
            Select the while-tile with the highest h value, say X
            If the h(X):= 0 and it does not have the blank space to its left then,
                Select another while-tile with the next-highest h value, say X
            Selector := black
        Else
            Select the black-tile with the highest h value, say X
            If the h(X):= 0 and it does not have the blank space to its left then,
                Select another black-tile with the next-highest h value, say X
            Selector:= white
 
       If X and the blank space is in 2-tiles distance then,
           Move X to the blank space and record the cost
 
       Foreach black tile b
           do h(b):= tile-distance to the first white tile to its right
       Foreach white tile w
           do h(w):= tile-distance to the first black tile to its left * -1
End while

Let’s go through the Steps

Step 1: Selector: Null – INITIAL STATE

Heuristics432-2-3-4
TilesBBBWWW

cost = 0

Step 2: Selector: W

Heuristics321-1-3-4
TilesBBBWWW

cost = 1

Step 3: Selector: B

Heuristics31-11-1-2
TilesBBWBWW

cost = 3

Step 4: Selector: W

Heuristics1-131-1-2
TilesBWBBWW

cost = 2

Step 5: Selector: B

Heuristics1-121-1-2
TilesBWBBWW

cost = 1

Step 6: Selector: W

Heuristics1-1-232-2
TilesBWWBBW

cost = 3

Step 7: Selector: B

Heuristics1-1-221-1
TilesBWWBBW

cost = 2

Step 8: Selector: W

Heuristics1-1-2-121
TilesBWWWBB

cost = 3

Step 9: Selector: B

Heuristics1-1-2-300
TilesBWWWBB

cost = 2

Step 10: Selector: W

Heuristics2-2-3-400
TilesBWWWBB

cost = 3

Step 11: Selector: B

Heuristics1-1-2-300
TilesBWWWBB

cost = 1

Step 12: Selector: W

Heuristics02-2-300
TilesWBWWBB

cost = 2

Step 13: Selector: B

Heuristics01-1-200
TilesWBWWBB

cost = 1

Step 14: Selector: W

Heuristics001-200
TilesWWBWBB

cost = 2

Step 15: Selector: B

Heuristics001-100
TilesWWBWBB

cost = 1

Step 16: Selector: W

Heuristics000100
TilesWWWBBB

cost = 2

Conclusion

In conclusion, the algorithm “3 white and 3 black tiles problem” stands as my proposed solution to the problem at hand. If you find any issues or if you have a better solution, I warmly encourage you to share your insights in the comments. Together, we can refine and enhance our understanding, ensuring optimal outcomes.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *