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.
Table of Contents
The Approach
- Analyze the state-space of the problem.
- 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
Positions | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Tiles | B | B | B | W | W | W |
Goal State
Positions | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Tiles | W | W | W | B | B | B |
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
Heuristics | 4 | 3 | 2 | -2 | -3 | -4 | |
Tiles | B | B | B | W | W | W |
cost = 0
Step 2: Selector: W
Heuristics | 3 | 2 | 1 | -1 | -3 | -4 | |
Tiles | B | B | B | W | W | W |
cost = 1
Step 3: Selector: B
Heuristics | 3 | 1 | -1 | 1 | -1 | -2 | |
Tiles | B | B | W | B | W | W |
cost = 3
Step 4: Selector: W
Heuristics | 1 | -1 | 3 | 1 | -1 | -2 | |
Tiles | B | W | B | B | W | W |
cost = 2
Step 5: Selector: B
Heuristics | 1 | -1 | 2 | 1 | -1 | -2 | |
Tiles | B | W | B | B | W | W |
cost = 1
Step 6: Selector: W
Heuristics | 1 | -1 | -2 | 3 | 2 | -2 | |
Tiles | B | W | W | B | B | W |
cost = 3
Step 7: Selector: B
Heuristics | 1 | -1 | -2 | 2 | 1 | -1 | |
Tiles | B | W | W | B | B | W |
cost = 2
Step 8: Selector: W
Heuristics | 1 | -1 | -2 | -1 | 2 | 1 | |
Tiles | B | W | W | W | B | B |
cost = 3
Step 9: Selector: B
Heuristics | 1 | -1 | -2 | -3 | 0 | 0 | |
Tiles | B | W | W | W | B | B |
cost = 2
Step 10: Selector: W
Heuristics | 2 | -2 | -3 | -4 | 0 | 0 | |
Tiles | B | W | W | W | B | B |
cost = 3
Step 11: Selector: B
Heuristics | 1 | -1 | -2 | -3 | 0 | 0 | |
Tiles | B | W | W | W | B | B |
cost = 1
Step 12: Selector: W
Heuristics | 0 | 2 | -2 | -3 | 0 | 0 | |
Tiles | W | B | W | W | B | B |
cost = 2
Step 13: Selector: B
Heuristics | 0 | 1 | -1 | -2 | 0 | 0 | |
Tiles | W | B | W | W | B | B |
cost = 1
Step 14: Selector: W
Heuristics | 0 | 0 | 1 | -2 | 0 | 0 | |
Tiles | W | W | B | W | B | B |
cost = 2
Step 15: Selector: B
Heuristics | 0 | 0 | 1 | -1 | 0 | 0 | |
Tiles | W | W | B | W | B | B |
cost = 1
Step 16: Selector: W
Heuristics | 0 | 0 | 0 | 1 | 0 | 0 | |
Tiles | W | W | W | B | B | B |
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.