Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: bradyawn
- PROG: contest
- LANG: C++11
- */
- #include <iostream>
- #include <algorithm>
- #include <iomanip>
- #include <fstream>
- #include <vector>
- #include <deque>
- #include <string>
- #include <cmath>
- #include <map>
- #include <unordered_map>
- #include <utility>
- #include <set>
- #include <unordered_set>
- #include <ctime>
- #include <queue>
- #include <stack>
- #include <bitset>
- #include <random>
- #include <cstring>
- #include <complex>
- #include <cassert>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef pair<int,int> i2;
- typedef pair<ll,ll> ll2;
- int n;
- int val[11][11];
- i2 pos[101];
- struct S
- {
- i2 d; //moves, swaps
- int type;
- int cur;
- int lvl;
- S(int mm, int ss, int tt, int cc, int lv)
- {
- d.first = mm;
- d.second = ss;
- type = tt;
- cur = cc;
- lvl = lv;
- }
- friend bool operator<(const S &lhs, const S &rhs)
- {
- return lhs.d > rhs.d;
- }
- };
- //TP, cur, level
- i2 dist[3][101][101];
- priority_queue<S> pq;
- bool f(int x) //x e [1,n]
- {
- return (1 <= x && x <= n);
- }
- int main()
- {
- //ifstream inf("");
- //ofstream outf("");
- //cout << setprecision(10);
- ios::sync_with_stdio(0); cin.tie(0);
- memset(dist, -1, sizeof dist);
- cin >> n;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++) {cin >> val[i][j]; pos[val[i][j]] = {i,j}; }
- //S(MOVES, SWITCHES, TYPE, CUR, LEVEL)
- // TYPE 0 = KNIGHT
- // TYPE 1 = ROOK
- // TYPE 2 = BISHOP
- pq.push(S(0, 0, 0, 1, 1));
- pq.push(S(0, 0, 1, 1, 1));
- pq.push(S(0, 0, 2, 1, 1));
- while (!pq.empty())
- {
- auto a = pq.top(); pq.pop();
- int row = pos[a.cur].first;
- int col = pos[a.cur].second;
- if (a.cur == a.lvl+1) a.lvl++;
- if (dist[a.type][a.cur][a.lvl].first != -1) continue;
- dist[a.type][a.cur][a.lvl] = a.d;
- if (a.lvl == n*n && a.cur == n*n)
- {
- cout << a.d.first << ' ' << a.d.second << '\n';
- return 0;
- }
- //repositioning
- if (a.type == 0) // TYPE 0 = KNIGHT
- {
- S b = a; b.d.first++;
- if (f(row+2) && f(col+1)) {b.cur = val[row+2][col+1]; pq.push(b);}
- if (f(row+2) && f(col-1)) {b.cur = val[row+2][col-1]; pq.push(b);}
- if (f(row-2) && f(col+1)) {b.cur = val[row-2][col+1]; pq.push(b);}
- if (f(row-2) && f(col-1)) {b.cur = val[row-2][col-1]; pq.push(b);}
- if (f(row+1) && f(col+2)) {b.cur = val[row+1][col+2]; pq.push(b);}
- if (f(row+1) && f(col-2)) {b.cur = val[row+1][col-2]; pq.push(b);}
- if (f(row-1) && f(col+2)) {b.cur = val[row-1][col+2]; pq.push(b);}
- if (f(row-1) && f(col-2)) {b.cur = val[row-1][col-2]; pq.push(b);}
- }
- else if (a.type == 1) // TYPE 1 = ROOK
- {
- S b = a; b.d.first++;
- for (int i = 1; i <= n; i++) if (i != row) { b.cur = val[i][col]; pq.push(b); }
- for (int i = 1; i <= n; i++) if (i != col) { b.cur = val[row][i]; pq.push(b); }
- }
- else // TYPE 2 = BISHOP
- {
- S b = a; b.d.first++;
- for (int i = 1; i <= n; i++)
- {
- if (f(row+i) && f(col+i)) { b.cur = val[row+i][col+i]; pq.push(b); }
- if (f(row+i) && f(col-i)) { b.cur = val[row+i][col-i]; pq.push(b); }
- if (f(row-i) && f(col+i)) { b.cur = val[row-i][col+i]; pq.push(b); }
- if (f(row-i) && f(col-i)) { b.cur = val[row-i][col-i]; pq.push(b); }
- }
- }
- //swapping out pieces, switch++
- a.d.first++;
- a.d.second++;
- a.type++; a.type %= 3;
- pq.push(a);
- a.type++; a.type %= 3;
- pq.push(a);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment