Advertisement
aimon1337

Untitled

Jan 9th, 2022
1,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. #include <algorithm>
  2. #include <array>
  3. #include <bitset>
  4. #include <cassert>
  5. #include <chrono>
  6. #include <cmath>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <fstream>
  10. #include <functional>
  11. #include <iomanip>
  12. #include <iostream>
  13. #include <map>
  14. #include <numeric>
  15. #include <queue>
  16. #include <random>
  17. #include <set>
  18. #include <stdexcept>
  19. #include <stdlib.h>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <utility>
  23. #include <vector>
  24. using namespace std;
  25.  
  26. template<typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }
  27. template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type > ostream & operator<<(ostream& os, const T_container& v) { os << '{'; string sep; for (const T& x : v) os << sep << x, sep = ", "; return os << '}'; }
  28.  
  29. void dbg_out() { cerr << endl; }
  30. template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  31. #ifdef NEAL_DEBUG
  32. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  33. #else
  34. #define dbg(...)
  35. #endif
  36.  
  37.  
  38. //ACTION = 0 -> UP; 1-> DOWN; 2 - > LEFT 3-> RIGHT
  39.  
  40. int getInversion(vector<int> arr)
  41. {
  42.     int inv_count = 0;
  43.     for (int i = 0; i < arr.size() - 1; ++i)
  44.     {
  45.         for (int j = i + 1; j < arr.size(); ++j)
  46.         {
  47.             inv_count += ((arr[i] > arr[j]) ? 1 : 0);
  48.         }
  49.     }
  50.     return inv_count;
  51. }
  52.  
  53. bool isSolvable(vector<vector<int>> curState, int N)
  54. {
  55.     int empty_row = -1;
  56.     vector<int> arr;
  57.     for (int i = 0 ; i < N; ++i)
  58.     {
  59.         for (int j = 0; j < N; ++j)
  60.         {
  61.             if (curState[i][j])
  62.                 arr.push_back(curState[i][j]);
  63.             else empty_row = i;
  64.         }
  65.     }
  66.     int inversions = getInversion(arr);
  67.     if (N % 2 == 0 && (empty_row % 2 == !(inversions % 2)))
  68.         return true;
  69.     if (N % 2 == 1 && inversions % 2 == 0)
  70.         return true;
  71.     return false;
  72. }
  73.  
  74. vector<vector<int>> readData(string filePath, int &N)
  75. {
  76.     vector<vector<int>> givenState;
  77.     cout << "Care este calea fisierului de intrare: ";
  78.     cin >> filePath;
  79.     ifstream fin;
  80.     fin.open(filePath, ios::in);
  81.     if (!fin)
  82.     {
  83.         cerr << "Eroare la deschiderea fisierului " << filePath;
  84.         exit(EXIT_FAILURE);
  85.     }
  86.     fin >> N;
  87.     int i, j, x;
  88.     for (i = 0 ; i < N; ++i)
  89.     {
  90.         vector<int> currentRow;
  91.         for (j = 0; j < N; ++j)
  92.         {
  93.             fin >> x;
  94.             currentRow.push_back(x);
  95.         }
  96.         givenState.push_back(currentRow);
  97.     }
  98.     fin.close();
  99.     return givenState;
  100. }
  101.  
  102. void printData(vector<vector<int>> curState, string outputFilePath)
  103. {
  104.     int N = curState.size();
  105.     ofstream fout(outputFilePath, ios::out | ios::app);
  106.     for (int i = 0 ; i < N; ++i)
  107.     {
  108.         for (int j = 0; j < N; ++j)
  109.         {
  110.             fout << curState[i][j] << " ";
  111.         }
  112.         fout << endl;
  113.     }
  114.     fout << endl;
  115.     fout.close();
  116. }
  117.  
  118. void getEmptyPosition(vector<vector<int>> curState, int N, int &xPoz, int &yPoz)
  119. {
  120.     for (int i = 0; i < N ; ++i)
  121.     {
  122.         for (int j = 0; j < N; ++j)
  123.         {
  124.             if (curState[i][j] == 0)
  125.             {
  126.                 xPoz = i, yPoz = j;
  127.                 return;
  128.             }
  129.         }
  130.     }
  131. }
  132.  
  133. bool canMoveUp(vector<vector<int>> curState, int N, int xPoz, int yPoz)
  134. {
  135.     for (int i = 0; i < N; ++i)
  136.     {
  137.         if (curState[xPoz][yPoz]  == curState[0][i])
  138.             return false;
  139.     }
  140.     return true;
  141. }
  142.  
  143.  
  144. bool canMoveDown(vector<vector<int>> curState, int N, int xPoz, int yPoz)
  145. {
  146.     for (int i = 0; i < N; ++i)
  147.     {
  148.         if (curState[xPoz][yPoz]  == curState[N - 1][i])
  149.             return false;
  150.     }
  151.     return true;
  152. }
  153.  
  154.  
  155. bool canMoveLeft(vector<vector<int>> curState, int N, int xPoz, int yPoz)
  156. {
  157.     for (int i = 0; i < N; ++i)
  158.     {
  159.         if (curState[xPoz][yPoz]  == curState[i][0])
  160.             return false;
  161.     }
  162.     return true;
  163. }
  164.  
  165.  
  166. bool canMoveRight(vector<vector<int>> curState, int N, int xPoz, int yPoz)
  167. {
  168.     for (int i = 0; i < N; ++i)
  169.     {
  170.         if (curState[xPoz][yPoz]  == curState[i][N - 1])
  171.             return false;
  172.     }
  173.     return true;
  174. }
  175.  
  176. void expandState(vector<vector<int>> curState, int N, vector<vector<int>> &child1, vector<vector<int>> &child2, vector<vector<int>> &child3, vector<vector<int>> &child4)
  177. {
  178.     //child1 - up child2 - down child3 - left child4 - right
  179.     int xPoz, yPoz;
  180.     getEmptyPosition(curState, N, xPoz, yPoz);
  181.     if (canMoveUp(curState, N, xPoz, yPoz))
  182.     {
  183.         child1 = curState;
  184.         swap(child1[xPoz - 1][yPoz], child1[xPoz][yPoz]);
  185.  
  186.     }
  187.     if (canMoveDown(curState, N, xPoz, yPoz))
  188.     {
  189.         child2 = curState;
  190.         swap(child2[xPoz + 1][yPoz], child2[xPoz][yPoz]);
  191.     }
  192.     if (canMoveLeft(curState, N, xPoz, yPoz))
  193.     {
  194.         child3 = curState;
  195.         swap(child3[xPoz][yPoz - 1], child3[xPoz][yPoz]);
  196.     }
  197.     if (canMoveRight(curState, N, xPoz, yPoz))
  198.     {
  199.         child4 = curState;
  200.         swap(child4[xPoz][yPoz + 1], child4[xPoz][yPoz]);
  201.     }
  202. }
  203.  
  204.  
  205. void getChilds(vector<vector<int>> curState, int N, int &level, vector<pair<vector<vector<int>>, int>> &test, string outputFilePath)
  206. {
  207.     level++;
  208.     vector<vector<int>> child1, child2, child3, child4;
  209.     expandState(curState, N, child1, child2, child3, child4);
  210.     dbg(child1, child2, child3, child4);
  211.     if (!child1.empty())
  212.     {
  213.         printData(child1, outputFilePath);
  214.         test.push_back(make_pair(child1, level));
  215.     }
  216.     if (!child2.empty())
  217.     {
  218.         printData(child2, outputFilePath);
  219.         test.push_back(make_pair(child2, level));
  220.     }
  221.     if (!child3.empty())
  222.     {
  223.         printData(child3, outputFilePath);
  224.         test.push_back(make_pair(child3, level));
  225.     }
  226.     if (!child4.empty())
  227.     {
  228.         printData(child4, outputFilePath);
  229.         test.push_back(make_pair(child4, level));
  230.     }
  231.     dbg(level);
  232.     for (auto itr = test.begin(); itr != test.end(); ++itr) {
  233.         cout << *itr << "\n";
  234.     }
  235. }
  236.  
  237.  
  238. bool checkForDuplicates(vector<pair<vector<vector<int>>, int>> Tree)
  239. {
  240.     for (auto itr = Tree.begin(); itr < Tree.end(); ++itr)
  241.     {
  242.         for (auto itr2 = itr + 1; itr2 < Tree.end(); ++itr2)
  243.         {
  244.             if ((itr->first) == (itr2->first))
  245.             {
  246.                 return false;
  247.             }
  248.         }
  249.     }
  250.     return true;
  251. }
  252.  
  253. int main()
  254. {
  255.     int N;
  256.     vector<vector<int>> curState;
  257.     string inputFilePath, outputFilePath;
  258.     curState = readData(inputFilePath, N);
  259.     dbg(curState);
  260.     if (isSolvable(curState, N))
  261.     {
  262.         cout << "\nCare este calea fisierului de iesire: ";
  263.         cin >> outputFilePath;
  264.         printData(curState, outputFilePath);
  265.         int level = 0;
  266.         vector<pair<vector<vector<int>>, int>> Tree;
  267.         Tree.push_back(make_pair(curState, level));
  268.         dbg(Tree);
  269.         getChilds(curState, N, level, Tree, outputFilePath);
  270.         if (checkForDuplicates(Tree) == false)
  271.         {
  272.             cout << "Au fost gasite duplicate";
  273.         }
  274.         else cout << "Nu exista duplicate";
  275.         return 0;
  276.     }
  277.     else
  278.     {
  279.         cerr << "Puzzle-ul introdus nu este rezolvabil!";
  280.         exit(EXIT_FAILURE);
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement