How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
You are given a 2x3 sliding puzzle grid. The goal is to move the tiles to match the target state "123450" (where 0 represents the blank space). You can only move tiles adjacent to the blank space into the blank space. The task is to find the minimum number of moves to reach the target state or return -1 if it is impossible.
String target = "123450";
String start = "";
target string represents the goal configuration of the puzzle.start string is constructed by concatenating the given 2D array board into a single string. This makes it easier to manipulate states in the BFS algorithm.int[][] dirs = new int[][] {
{ 1, 3 },
{ 0, 2, 4 },
{ 1, 5 },
{ 0, 4 },
{ 1, 3, 5 },
{ 2, 4 }
};
0).dirs[i] contains the indices that the blank space (0) can swap with for the corresponding position i.Queue<String> queue = new LinkedList<>();
HashSet<String> visited = new HashSet<>();
queue.offer(start);
visited.add(start);
int res = 0;
queue is used for BFS to explore all possible states level by level.HashSet visited is used to track states that have already been processed to avoid redundant calculations.res keeps track of the number of moves taken so far.while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
String cur = queue.poll();
if (cur.equals(target)) {
return res;
}
size variable ensures that each level of moves is processed in one iteration.cur matches the target, the function returns res (the number of moves).int zero = cur.indexOf('0');
for (int dir : dirs[zero]) {
String next = swap(cur, zero, dir);
if (visited.contains(next)) {
continue;
}
visited.add(next);
queue.offer(next);
}
0) in the current state (cur).dirs to determine possible swaps.next and:
private String swap(String str, int i, int j) {
StringBuilder sb = new StringBuilder(str);
sb.setCharAt(i, str.charAt(j));
sb.setCharAt(j, str.charAt(i));
return sb.toString();
}
i and j in the string to create a new board state.return -1;
-1 to indicate that it is impossible to reach the target state.Input:
int[][] board = {
{1, 2, 3},
{4, 0, 5}
};
Output:
1
Explanation: Swap 0 and 5 to achieve the target state.
Input:
int[][] board = {
{1, 2, 3},
{5, 4, 0}
};
Output:
-1
Explanation: It is impossible to reach the target state from this configuration.
Input:
int[][] board = {
{4, 1, 2},
{5, 0, 3}
};
Output:
5
Explanation: Requires 5 moves to achieve the target state.
Input:
int[][] board = {
{1, 2, 3},
{0, 4, 5}
};
Output:
2
Explanation: Swap 0 with 4 and then with 5 to reach the target.
Already Solved: Input:
int[][] board = {
{1, 2, 3},
{4, 5, 0}
};
Output:
0
Explanation: The board is already in the target state.
Unsolvable Puzzle: Input:
int[][] board = {
{3, 2, 1},
{4, 5, 0}
};
Output:
-1
Explanation: The puzzle configuration cannot be solved.
🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
🔒 Locked steps cannot be moved. Only unlocked steps are draggable.
🔊 Enable sound for swap feedback and completion effects.