Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
- #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++);
- #define cout_map(mp) for(auto& [f, s] : mp) cout << f << " " << s << "\n";
- #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
- #define fixed(n) fixed << setprecision(n)
- #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
- #define fill(vec, value) memset(vec, value, sizeof(vec));
- #define Num_of_Digits(n) ((int)log10(n) + 1)
- #define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
- #define all(vec) vec.begin(), vec.end()
- #define rall(vec) vec.rbegin(), vec.rend()
- #define sz(x) int(x.size())
- #define debug(x) cout << #x << ": " << (x) << "\n";
- #define fi first
- #define se second
- #define Pair pair < int, int >
- #define ll long long
- #define ull unsigned long long
- #define Mod 1'000'000'007
- #define OO 2'000'000'000
- #define EPS 1e-9
- #define PI acos(-1)
- template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
- for (auto &x: v) in >> x;
- return in;
- }
- template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
- for (const T &x: v) out << x << ' ';
- return out;
- }
- void AhMeD_HoSSaM(){
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- vector < vector < ll > > adj(10);
- vector < ll > puzzle(10), cost(10);
- bool check(vector < ll >& p){
- for(int i = 1; i <= 8; i++)
- if(p[i] != i)
- return false;
- return true;
- }
- ll BFS(ll start){
- if(check(puzzle)) return 0;
- queue < pair < pair < ll, ll > , vector < ll > > > bfs;
- map < vector < ll >, ll > path;
- bfs.push({{start, 0ll}, puzzle});
- path.insert({puzzle, 0ll});
- while(!bfs.empty()){
- int sz = sz(bfs);
- while(sz--){
- auto [node, curr_vec] = bfs.front();
- ll curr_node = node.first, curr_cost = node.second;
- bfs.pop();
- for(auto& new_node : adj[curr_node]){
- swap(curr_vec[curr_node], curr_vec[new_node]);
- if(!path.count(curr_vec) || path[curr_vec] > curr_cost + cost[curr_vec[curr_node]]){
- path[curr_vec] = curr_cost + cost[curr_vec[curr_node]];
- bfs.push({{new_node, curr_cost + cost[curr_vec[curr_node]]}, curr_vec});
- }
- swap(curr_vec[new_node], curr_vec[curr_node]);
- }
- }
- }
- if(!path.count({0, 1, 2, 3, 4, 5, 6, 7 ,8 ,0})) return -1;
- return path[{0, 1, 2, 3, 4, 5, 6, 7 ,8 ,0}];
- }
- void Solve(){
- 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}};
- for(int i = 0, sq; i < 3; i++)
- for(int j = 1; j <= 3 && cin >> sq; j++)
- puzzle[i * 3 + j] = sq;
- for(ll i = 1; i <= 8 && cin >> cost[i]; i++);
- ll beg = -1;
- for(int i = 1; i <= 9; i++)
- if(puzzle[i] == 0)
- beg = i;
- ll ans = BFS(beg);
- cout << ans << "\n";
- }
- int main(){
- AhMeD_HoSSaM();
- int t = 1;
- //cin >> t;
- while(t--)
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement