|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +#define N 3 // for 3 x 3 tile size |
| 5 | + |
| 6 | +class Node { |
| 7 | +private: |
| 8 | + vector<vector<int>> state; |
| 9 | + pair<int, int> blankIndex; |
| 10 | + Node *parent; |
| 11 | + int depth; |
| 12 | + |
| 13 | + pair<int, int> findBlankIndex() { |
| 14 | + for (int i = 0; i < N; i++) { |
| 15 | + for (int j = 0; j < N; j++) { |
| 16 | + if (state[i][j] == 0) { |
| 17 | + return {i, j}; |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + return {-1, -1}; |
| 22 | + } |
| 23 | + |
| 24 | +public: |
| 25 | + Node(const vector<vector<int>> &state, Node *parent, int depth) |
| 26 | + : state(state), parent(parent), depth(depth) { |
| 27 | + blankIndex = findBlankIndex(); |
| 28 | + } |
| 29 | + |
| 30 | + vector<vector<int>> getState() { return state; } |
| 31 | + pair<int, int> getBlankIndex() { return blankIndex; } |
| 32 | + int getDepth() { return depth; } |
| 33 | +}; |
| 34 | + |
| 35 | +class Solve8TilePuzzle { |
| 36 | +public: |
| 37 | + vector<vector<int>> initialState, goalState; |
| 38 | + set<vector<vector<int>>> visitedList; |
| 39 | + |
| 40 | + Solve8TilePuzzle(const vector<vector<int>> &initialState, const vector<vector<int>> &goalState) |
| 41 | + : initialState(initialState), goalState(goalState) {} |
| 42 | + |
| 43 | + bool boundsOK(int i, int j) { |
| 44 | + return (i >= 0 && i < N && j >= 0 && j < N); |
| 45 | + } |
| 46 | + |
| 47 | + bool dls(Node *currNode, int depthLimit) { |
| 48 | + if (currNode->getDepth() > depthLimit) return false; |
| 49 | + visitedList.insert(currNode->getState()); |
| 50 | + |
| 51 | + if (currNode->getState() == goalState) { |
| 52 | + printSolution(currNode); |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + const vector<vector<int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; |
| 57 | + for (const auto &dir : dirs) { |
| 58 | + int newI = currNode->getBlankIndex().first + dir[0]; |
| 59 | + int newJ = currNode->getBlankIndex().second + dir[1]; |
| 60 | + |
| 61 | + if (!boundsOK(newI, newJ)) continue; |
| 62 | + |
| 63 | + vector<vector<int>> newState = currNode->getState(); |
| 64 | + swap(newState[currNode->getBlankIndex().first][currNode->getBlankIndex().second], newState[newI][newJ]); |
| 65 | + Node *newNode = new Node(newState, currNode, currNode->getDepth() + 1); |
| 66 | + |
| 67 | + if (visitedList.find(newNode->getState()) == visitedList.end()) { |
| 68 | + if (dls(newNode, depthLimit)) return true; |
| 69 | + } |
| 70 | + } |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + void iterativeDeepeningSearch() { |
| 75 | + for (int depth = 0; depth < INT_MAX; depth++) { |
| 76 | + visitedList.clear(); |
| 77 | + Node *root = new Node(initialState, nullptr, 0); |
| 78 | + if (dls(root, depth)) return; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + void printSolution(Node *node) { |
| 83 | + cout << "Solution found at depth " << node->getDepth() << endl; |
| 84 | + // Print the path (not implemented here for simplicity) |
| 85 | + } |
| 86 | + |
| 87 | + bool isSolvable() { |
| 88 | + int inversions = 0; |
| 89 | + for (int i = 0; i < N * N; i++) { |
| 90 | + for (int j = i + 1; j < N * N; j++) { |
| 91 | + if (initialState[i / N][i % N] && initialState[j / N][j % N] && |
| 92 | + initialState[i / N][i % N] > initialState[j / N][j % N]) { |
| 93 | + inversions++; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return (inversions % 2 == 0); |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +void takeUserInput(vector<vector<int>> &initialState) { |
| 102 | + cout << "Enter the initial state of the 8-tile puzzle (0 for blank tile):\n"; |
| 103 | + for (int i = 0; i < N; i++) { |
| 104 | + for (int j = 0; j < N; j++) { |
| 105 | + cin >> initialState[i][j]; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +int main() { |
| 111 | + vector<vector<int>> initialState(N, vector<int>(N, 0)); |
| 112 | + vector<vector<int>> goalState = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}}; |
| 113 | + |
| 114 | + takeUserInput(initialState); |
| 115 | + |
| 116 | + Solve8TilePuzzle solvePuzzle(initialState, goalState); |
| 117 | + if (solvePuzzle.isSolvable()) { |
| 118 | + solvePuzzle.iterativeDeepeningSearch(); |
| 119 | + } else { |
| 120 | + cout << "\nNot Solvable" << endl; |
| 121 | + } |
| 122 | + |
| 123 | + return 0; |
| 124 | +} |
0 commit comments