1
1
#include < bits/stdc++.h>
2
2
using namespace std ;
3
3
4
- #define N 3 // for 3 x 3 tile size
4
+ #define N 3 // Define the size of the tile puzzle (3x3)
5
5
6
+ /* *
7
+ * @class Node
8
+ * @brief Represents a state in the 8-tile puzzle.
9
+ */
6
10
class Node {
7
11
private:
8
- vector<vector<int >> state;
9
- pair<int , int > blankIndex;
10
- Node *parent;
11
- int depth;
12
-
12
+ vector<vector<int >> state; // /< Current state of the puzzle
13
+ pair<int , int > blankIndex; // /< Index of the blank tile (0)
14
+ Node *parent; // /< Parent node for tracing back the solution
15
+ int depth; // /< Depth of the current node in the search tree
16
+
17
+ /* *
18
+ * @brief Finds the index of the blank tile in the current state.
19
+ * @return Pair of indices (row, column) of the blank tile.
20
+ */
13
21
pair<int , int > findBlankIndex () {
14
22
for (int i = 0 ; i < N; i++) {
15
23
for (int j = 0 ; j < N; j++) {
@@ -18,32 +26,53 @@ class Node {
18
26
}
19
27
}
20
28
}
21
- return {-1 , -1 };
29
+ return {-1 , -1 }; // Should not reach here if the state is valid
22
30
}
23
31
24
32
public:
33
+ /* *
34
+ * @brief Constructor for Node class.
35
+ * @param state Initial state of the node.
36
+ * @param parent Pointer to the parent node.
37
+ * @param depth Depth of the node.
38
+ */
25
39
Node (const vector<vector<int >> &state, Node *parent, int depth)
26
40
: state(state), parent(parent), depth(depth) {
27
41
blankIndex = findBlankIndex ();
28
42
}
29
43
30
- vector<vector<int >> getState () { return state; }
31
- pair<int , int > getBlankIndex () { return blankIndex; }
32
- int getDepth () { return depth; }
44
+ vector<vector<int >> getState () const { return state; }
45
+ pair<int , int > getBlankIndex () const { return blankIndex; }
46
+ int getDepth () const { return depth; }
33
47
};
34
48
49
+ /* *
50
+ * @class Solve8TilePuzzle
51
+ * @brief Solves the 8-tile puzzle using iterative deepening search.
52
+ */
35
53
class Solve8TilePuzzle {
36
54
public:
37
- vector<vector<int >> initialState, goalState;
38
- set<vector<vector<int >>> visitedList;
39
-
55
+ vector<vector<int >> initialState, goalState; // /< Initial and goal states
56
+ set<vector<vector<int >>> visitedList; // /< Set to track visited states
57
+
58
+ /* *
59
+ * @brief Constructor for Solve8TilePuzzle class.
60
+ * @param initialState Initial state of the puzzle.
61
+ * @param goalState Goal state of the puzzle.
62
+ */
40
63
Solve8TilePuzzle (const vector<vector<int >> &initialState, const vector<vector<int >> &goalState)
41
64
: initialState(initialState), goalState(goalState) {}
42
65
43
- bool boundsOK (int i, int j) {
66
+ bool boundsOK (int i, int j) const {
44
67
return (i >= 0 && i < N && j >= 0 && j < N);
45
68
}
46
69
70
+ /* *
71
+ * @brief Performs depth-limited search.
72
+ * @param currNode Current node being explored.
73
+ * @param depthLimit Maximum depth to explore.
74
+ * @return True if goal state is found, false otherwise.
75
+ */
47
76
bool dls (Node *currNode, int depthLimit) {
48
77
if (currNode->getDepth () > depthLimit) return false ;
49
78
visitedList.insert (currNode->getState ());
@@ -71,6 +100,9 @@ class Solve8TilePuzzle {
71
100
return false ;
72
101
}
73
102
103
+ /* *
104
+ * @brief Initiates the iterative deepening search to solve the puzzle.
105
+ */
74
106
void iterativeDeepeningSearch () {
75
107
for (int depth = 0 ; depth < INT_MAX; depth++) {
76
108
visitedList.clear ();
@@ -79,12 +111,20 @@ class Solve8TilePuzzle {
79
111
}
80
112
}
81
113
114
+ /* *
115
+ * @brief Prints the solution path.
116
+ * @param node The node at which the solution is found.
117
+ */
82
118
void printSolution (Node *node) {
83
119
cout << " Solution found at depth " << node->getDepth () << endl;
84
- // Print the path (not implemented here for simplicity)
120
+ // Trace back to print the solution path if necessary
85
121
}
86
122
87
- bool isSolvable () {
123
+ /* *
124
+ * @brief Checks if the puzzle is solvable based on inversions.
125
+ * @return True if solvable, false otherwise.
126
+ */
127
+ bool isSolvable () const {
88
128
int inversions = 0 ;
89
129
for (int i = 0 ; i < N * N; i++) {
90
130
for (int j = i + 1 ; j < N * N; j++) {
@@ -98,6 +138,10 @@ class Solve8TilePuzzle {
98
138
}
99
139
};
100
140
141
+ /* *
142
+ * @brief Takes user input for the initial state of the puzzle.
143
+ * @param initialState Reference to the 2D vector where the input will be stored.
144
+ */
101
145
void takeUserInput (vector<vector<int >> &initialState) {
102
146
cout << " Enter the initial state of the 8-tile puzzle (0 for blank tile):\n " ;
103
147
for (int i = 0 ; i < N; i++) {
@@ -107,6 +151,9 @@ void takeUserInput(vector<vector<int>> &initialState) {
107
151
}
108
152
}
109
153
154
+ /* *
155
+ * @brief Main function to run the 8-tile puzzle solver.
156
+ */
110
157
int main () {
111
158
vector<vector<int >> initialState (N, vector<int >(N, 0 ));
112
159
vector<vector<int >> goalState = {{1 , 2 , 3 }, {4 , 5 , 6 }, {7 , 8 , 0 }};
0 commit comments