Sliding Block Puzzle Solver(2×3, 3×2, 3×3) – C++ Programming

A program written in c language to solve a 2×3, 3×2, 3×3 sliding block puzzle.

1. DFS (Brute-Force)
We can perform depth-first search on state space (Set of all configurations of a given problem i.e. all states that can be reached from initial state) tree.

Image source: https://courses.cs.washington.edu

In this solution, successive moves can take us away from the goal rather than bringing closer. The search of state space tree follows leftmost path from the root regardless of initial state. An answer node may never be found in this approach.

2. BFS (Brute-Force)
We can perform a Breadth-first search on state space tree. This always finds a goal state nearest to the root. But no matter what the initial state is, the algorithm attempts the same sequence of moves like DFS.

3. Branch and Bound
The search for an answer node can often be speeded by using an “intelligent” ranking function, also called an approximate cost function to avoid searching in sub-trees that do not contain an answer node. It is similar to backtracking technique but uses BFS-like search.

There are basically three types of nodes involved in Branch and Bound
1. Live node is a node that has been generated but whose children have not yet been generated.
2. E-node is a live node whose children are currently being explored. In other words, an E-node is a node currently being expanded.
3. Dead node is a generated node that is not to be expanded or explored any further. All children of a dead node have already been expanded.

Cost function:
Each node X in the search tree is associated with a cost. The cost function is useful for determining the next E-node. The next E-node is the one with least cost. The cost function is defined as,

C(X) = g(X) + h(X) where
g(X) = cost of reaching the current node from the root
h(X) = cost of reaching an answer node from X.


Ideal Cost function for 8-puzzle Algorithm :
We assume that moving one tile in any direction will have 1 unit cost. Keeping that in mind, we define cost function for 8-puzzle algorithm as below :

c(x) = f(x) + h(x) where
f(x) is the length of the path from root to x
(the number of moves so far) and
h(x) is the number of non-blank tiles not in
their goal position (the number of mis-
-placed tiles). There are at least h(x)
moves to transform state x to a goal state

An algorithm is available for getting an approximation of h(x) which is a unknown value.
Below diagram shows the path followed by above algorithm to reach final configuration from given initial configuration of 8-Puzzle. Note that only nodes having least value of cost function are expanded.

Download Full Code : https://github.com/Aloksmvdu/puzzleSolver/blob/master/puzzlesolver.cpp

...
struct Node
{
Node* parent;
int mat[R][C];
int x, y;
int cost;
int level;
};
int moveCost(int puzzle[R][C], int final[R][C])
{
int count = 0;
for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) if (puzzle[i][j] && puzzle[i][j] != final[i][j]) count++; return count; } int isSolvable(int x, int y) { return (x >= 0 && x < R && y >= 0 && y < C); } void Path(Node* root) { if (root == NULL) return; Path(root->parent);
display(root->mat);
printf("\n");
}

struct comp
{
bool operator()(const Node* lhs, const Node* rhs) const
{
return (lhs->cost + lhs->level) > (rhs->cost + rhs->level);
}
};

void solvePuzzle(int puzzle[R][C], int x, int y,int final[R][C])
{
priority_queue, comp> pq;
Node* root = newNode(puzzle, x, y, x, y, 0, NULL);
root->cost = moveCost(puzzle, final);
pq.push(root);
while (!pq.empty())
{
Node* min = pq.top();
pq.pop();
if (min->cost == 0)
{
Path(min);
return;
}
for (int i = 0; i < 4; i++) { if (isSolvable(min->x + row[i], min->y + col[i]))
{
Node* child = newNode(min->mat, min->x,
min->y, min->x + row[i],
min->y + col[i],
min->level + 1, min);
child->cost = moveCost(child->mat, final);
pq.push(child);
}
}
}
}