Skip to content

Commit 1499c7a

Browse files
committed
code updated according to the give comments
1 parent 40a93f4 commit 1499c7a

File tree

1 file changed

+63
-16
lines changed

1 file changed

+63
-16
lines changed

search/Iterative_deepening_search_8_tile_puzzle.cpp

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
#include <bits/stdc++.h>
22
using namespace std;
33

4-
#define N 3 // for 3 x 3 tile size
4+
#define N 3 // Define the size of the tile puzzle (3x3)
55

6+
/**
7+
* @class Node
8+
* @brief Represents a state in the 8-tile puzzle.
9+
*/
610
class Node {
711
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+
*/
1321
pair<int, int> findBlankIndex() {
1422
for (int i = 0; i < N; i++) {
1523
for (int j = 0; j < N; j++) {
@@ -18,32 +26,53 @@ class Node {
1826
}
1927
}
2028
}
21-
return {-1, -1};
29+
return {-1, -1}; // Should not reach here if the state is valid
2230
}
2331

2432
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+
*/
2539
Node(const vector<vector<int>> &state, Node *parent, int depth)
2640
: state(state), parent(parent), depth(depth) {
2741
blankIndex = findBlankIndex();
2842
}
2943

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; }
3347
};
3448

49+
/**
50+
* @class Solve8TilePuzzle
51+
* @brief Solves the 8-tile puzzle using iterative deepening search.
52+
*/
3553
class Solve8TilePuzzle {
3654
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+
*/
4063
Solve8TilePuzzle(const vector<vector<int>> &initialState, const vector<vector<int>> &goalState)
4164
: initialState(initialState), goalState(goalState) {}
4265

43-
bool boundsOK(int i, int j) {
66+
bool boundsOK(int i, int j) const {
4467
return (i >= 0 && i < N && j >= 0 && j < N);
4568
}
4669

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+
*/
4776
bool dls(Node *currNode, int depthLimit) {
4877
if (currNode->getDepth() > depthLimit) return false;
4978
visitedList.insert(currNode->getState());
@@ -71,6 +100,9 @@ class Solve8TilePuzzle {
71100
return false;
72101
}
73102

103+
/**
104+
* @brief Initiates the iterative deepening search to solve the puzzle.
105+
*/
74106
void iterativeDeepeningSearch() {
75107
for (int depth = 0; depth < INT_MAX; depth++) {
76108
visitedList.clear();
@@ -79,12 +111,20 @@ class Solve8TilePuzzle {
79111
}
80112
}
81113

114+
/**
115+
* @brief Prints the solution path.
116+
* @param node The node at which the solution is found.
117+
*/
82118
void printSolution(Node *node) {
83119
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
85121
}
86122

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 {
88128
int inversions = 0;
89129
for (int i = 0; i < N * N; i++) {
90130
for (int j = i + 1; j < N * N; j++) {
@@ -98,6 +138,10 @@ class Solve8TilePuzzle {
98138
}
99139
};
100140

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+
*/
101145
void takeUserInput(vector<vector<int>> &initialState) {
102146
cout << "Enter the initial state of the 8-tile puzzle (0 for blank tile):\n";
103147
for (int i = 0; i < N; i++) {
@@ -107,6 +151,9 @@ void takeUserInput(vector<vector<int>> &initialState) {
107151
}
108152
}
109153

154+
/**
155+
* @brief Main function to run the 8-tile puzzle solver.
156+
*/
110157
int main() {
111158
vector<vector<int>> initialState(N, vector<int>(N, 0));
112159
vector<vector<int>> goalState = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};

0 commit comments

Comments
 (0)