Advertisement
7oSkaaa

G. 8 Game

May 7th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
  6. #define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
  7. #define cout_map(mp) for(auto& [f, s] : mp) cout << f << "  " << s << "\n";
  8. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
  9. #define fixed(n) fixed << setprecision(n)
  10. #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  11. #define fill(vec, value) memset(vec, value, sizeof(vec));
  12. #define Num_of_Digits(n) ((int)log10(n) + 1)
  13. #define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
  14. #define all(vec) vec.begin(), vec.end()
  15. #define rall(vec) vec.rbegin(), vec.rend()
  16. #define sz(x) int(x.size())
  17. #define debug(x) cout << #x << ": " << (x) << "\n";
  18. #define fi first
  19. #define se second
  20. #define Pair pair < int, int >
  21. #define ll long long
  22. #define ull unsigned long long
  23. #define Mod  1'000'000'007
  24. #define OO 2'000'000'000
  25. #define EPS 1e-9
  26. #define PI acos(-1)
  27.  
  28. template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
  29.     for (auto &x: v) in >> x;
  30.     return in;
  31. }
  32.  
  33. template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
  34.     for (const T &x: v) out << x << ' ';
  35.     return out;
  36. }
  37.  
  38. void AhMeD_HoSSaM(){
  39.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  40.     #ifndef ONLINE_JUDGE
  41.         freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  42.     #endif
  43. }
  44.  
  45. vector < vector < ll > > adj(10);
  46. vector < ll > puzzle(10), cost(10);
  47.  
  48. bool check(vector < ll >& p){
  49.     for(int i = 1; i <= 8; i++)
  50.         if(p[i] != i)
  51.          return false;
  52.     return true;
  53. }
  54.  
  55. ll BFS(ll start){
  56.     if(check(puzzle)) return 0;
  57.     queue < pair < pair < ll, ll > , vector < ll > > > bfs;
  58.     map < vector < ll >, ll > path;
  59.     bfs.push({{start, 0ll}, puzzle});
  60.     path.insert({puzzle, 0ll});
  61.     while(!bfs.empty()){
  62.         int sz = sz(bfs);
  63.         while(sz--){
  64.             auto [node, curr_vec] = bfs.front();
  65.             ll curr_node = node.first, curr_cost = node.second;
  66.             bfs.pop();
  67.             for(auto& new_node : adj[curr_node]){
  68.                 swap(curr_vec[curr_node], curr_vec[new_node]);
  69.                 if(!path.count(curr_vec) || path[curr_vec] > curr_cost + cost[curr_vec[curr_node]]){
  70.                     path[curr_vec] = curr_cost + cost[curr_vec[curr_node]];
  71.                     bfs.push({{new_node, curr_cost + cost[curr_vec[curr_node]]}, curr_vec});
  72.                 }
  73.                 swap(curr_vec[new_node], curr_vec[curr_node]);
  74.             }
  75.         }
  76.     }
  77.     if(!path.count({0, 1, 2, 3, 4, 5, 6, 7 ,8 ,0})) return -1;
  78.     return path[{0, 1, 2, 3, 4, 5, 6, 7 ,8 ,0}];
  79. }
  80.  
  81. void Solve(){
  82.     adj = {{0}, {2, 4}, {1, 3, 5}, {2, 6}, {1, 7, 5}, {2, 4, 6, 8}, {3, 5, 9}, {4, 8}, {5, 7, 9}, {6, 8}};
  83.     for(int i = 0, sq; i < 3; i++)
  84.         for(int j = 1; j <= 3 && cin >> sq; j++)
  85.             puzzle[i * 3 + j] = sq;
  86.     for(ll i = 1; i <= 8 && cin >> cost[i]; i++);
  87.     ll beg = -1;
  88.     for(int i = 1; i <= 9; i++)
  89.         if(puzzle[i] == 0)
  90.             beg = i;
  91.     ll ans =  BFS(beg);
  92.     cout << ans << "\n";
  93. }
  94.  
  95. int main(){
  96.     AhMeD_HoSSaM();
  97.     int t = 1;
  98.     //cin >> t;
  99.     while(t--)
  100.         Solve();
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement