Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using ld = long double;
- using pii = pair<int, int>;
- constexpr int N = 100;
- tuple<bool, char, char> par[2][N][N];
- bool used[2][N][N];
- pii d[] = {{2, 1}, {2, -1}, {1, 2}, {-1, 2}, {-2, -1}, {-2, 1}, {-1, -2}, {1, -2}};
- int st1, st2, fn2, fn1;
- queue<tuple<bool, char, char>> q;
- vector<pair<bool, char>> ans;
- bool check(char x, char y) {
- return x >= 0 && x < 8 && y >= 0 && y < 8;
- }
- void bfs() {
- q.emplace(0, st1, st2);
- q.emplace(1, st1, st2);
- used[0][st1][st2] = 1;
- used[1][st1][st2] = 1;
- while(q.size()) {
- auto [t, p, w] = q.front();
- q.pop();
- char px = p / 8, py = p % 8;
- char wx = w / 8, wy = w % 8;
- if (t) {
- for (auto [dx, dy]: d) {
- if (check(wx + dx, wy + dy)) {
- char np = px * 8 + py;
- char nw = (wx + dx) * 8 + wy + dy;
- if (np != nw) {
- if (!used[1][np][nw]) {
- used[1][np][nw] = 1;
- q.emplace(1, np, nw);
- par[1][np][nw] = {t, p, w};
- }
- if (!used[0][np][nw]) {
- used[0][np][nw] = 1;
- q.emplace(0, np, nw);
- par[0][np][nw] = {t, p, w};
- }
- }
- }
- }
- }
- else {
- for (auto [dx, dy]: d) {
- if (check(px + dx, py + dy)) {
- char np = (px + dx) * 8 + py + dy;
- char nw = wx * 8 + wy;
- if (np != nw) {
- if (!used[1][np][nw]) {
- used[1][np][nw] = 1;
- q.emplace(1, np, nw);
- par[1][np][nw] = {t, p, w};
- }
- if (!used[0][np][nw]) {
- used[0][np][nw] = 1;
- q.emplace(0, np, nw);
- par[0][np][nw] = {t, p, w};
- }
- }
- }
- }
- }
- }
- }
- char c;
- int get_pos() {
- int ans = 0;
- cin >> c;
- ans = c - 'a';
- ans *= 8;
- cin >> c;
- ans += c - '1';
- return ans;
- }
- void print(char x) {
- char f = x / 8;
- char s = x % 8;
- f += 'a';
- s += '1';
- cout << f << s;
- }
- void Solve() {
- st1 = get_pos();
- st2 = get_pos();
- fn1 = get_pos();
- fn2 = get_pos();
- bfs();
- for (int i = 0, x = fn1, y = fn2; x != st1 || y != st2; ) {
- auto [ni, nx, ny] = par[i][x][y];
- if (ni)
- ans.emplace_back(1, y);
- else
- ans.emplace_back(0, x);
- i = ni;
- x = nx;
- y = ny;
- }
- reverse(ans.begin(), ans.end());
- for (auto [t, x]: ans) {
- cout << t + 1 << " ";
- print(x);
- cout << endl;
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- //auto start = chrono::high_resolution_clock::now();
- Solve();
- // auto end = chrono::high_resolution_clock::now();
- // cout << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement