Advertisement
coderbodrul

Untitled

May 5th, 2024
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | Gaming | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int mx = 1e6;
  4.  
  5. map<string, int> state_to_int; //mapping every state to unique value
  6. map<int, string> int_to_state; //getting the state for every int value
  7. string d; // destination state
  8. int ind = 0; // a variable for representing every state to int
  9. vector<int> adj[mx + 1];
  10. vector<int> par(mx + 1, -1);
  11. int dx[4] = {1, -1, 0, 0};
  12. int dy[4] = {0, 0, 1, -1};
  13. int vis[mx + 1];
  14.  
  15. void bfs(int strt) {
  16.     queue<int> q;
  17.     q.push(strt);
  18.  
  19.     while(!q.empty()) {
  20.         int fr = q.front();
  21.         string str = int_to_state[fr];
  22.         q.pop();
  23.         vis[fr] = 1;
  24.         if (str == d) {
  25.             return;
  26.         }
  27.  
  28.         int x, y, sx, sy;
  29.         for (int i = 0; i < 9; i++) {
  30.             if (str[i] == '0') {
  31.                 x = i / 3;
  32.                 y = i % 3;
  33.                 break;
  34.             }
  35.         }
  36.  
  37.         for (int i = 0; i < 4; i++) {
  38.             sx = x + dx[i];
  39.             sy = y + dy[i];
  40.             if (sx >= 0  and sx < 3 and sy >= 0  and sy < 3) {
  41.                 string tem = str;
  42.                 swap(tem[x * 3 + y], tem[sx * 3 + sy]);
  43.                 if (state_to_int.find(tem) == state_to_int.end()) {
  44.                     state_to_int[tem] = ind;
  45.                     int_to_state[ind] = tem;
  46.                     ind++;
  47.                     adj[fr].push_back(state_to_int[tem]);
  48.                     par[state_to_int[tem]] = fr;
  49.                 }
  50.             }
  51.         }
  52.         for (int i = 0; i < adj[fr].size(); i++) {
  53.             if (!vis[adj[fr][i]]) {
  54.                 q.push(adj[fr][i]);
  55.                 vis[adj[fr][i]] = 1;
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. int32_t main() {
  62.  
  63.     // define destination state & map the state to int
  64.     d = "123456780";
  65.     // taking input the source state
  66.     string s = "";
  67.     char c;
  68.     for (int i = 0; i < 3; i++) {
  69.         for (int j = 0; j < 3; j++) {
  70.             cin >> c;
  71.             s += c;
  72.         }
  73.     }
  74.  
  75.     state_to_int[s] = ind;
  76.     int_to_state[ind] = s;
  77.     ind++;
  78.  
  79.     memset(vis, 0, sizeof(vis));
  80.     bfs(state_to_int[s]);
  81.  
  82.     stack<int> st;
  83.     int node = state_to_int[d];
  84.     while(node != -1) {
  85.         st.push(node);
  86.         node = par[node];
  87.     }
  88.  
  89.     string str;
  90.     while(!st.empty()) {
  91.         str = int_to_state[st.top()];
  92.         st.pop();
  93.         cout << endl;
  94.         for (int i = 0; i < 9; i++) {
  95.             cout << str[i] << " \n"[i % 3 == 2];
  96.         }
  97.     }
  98.  
  99.     return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement