Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <queue>
- #include <string>
- #include <map>
- using namespace std;
- int dist[9][9][9][9];
- bool marked[9][9][9][9];
- pair<pair<int, int>, pair<int, int>> back[9][9][9][9];
- int main() {
- string b1, b2;
- string e1, e2;
- cin >> b1 >> b2 >> e1 >> e2;
- pair<int, int> s1 = { b1[0] - 'a' + 1, b1[1] - '0' };
- pair<int, int> s2 = { b2[0] - 'a' + 1, b2[1] - '0' };
- pair<int, int> s3 = { e1[0] - 'a' + 1, e1[1] - '0' };
- pair<int, int> s4 = { e2[0] - 'a' + 1, e2[1] - '0' };
- pair<pair<int, int>, pair<int, int>> start = { s1, s2 };
- pair<pair<int, int>, pair<int, int>> end = { s3, s4 };
- queue<pair<pair<int, int>, pair<int, int>>> queue;
- queue.push(start);
- marked[start.first.first][start.first.second][start.second.first][start.second.second] = 1;
- back[start.first.first][start.first.second][start.second.first][start.second.second] = { {- 1, - 1}, {- 1, -1} } ;
- while (!queue.empty()) {
- //
- pair<pair<int, int>, pair<int, int>> v = queue.front();
- if (v == end) {
- break;
- }
- if (v.first.first - 1 > 0 and v.first.second + 2 < 9 and v.second.first != (v.first.first - 1) and v.second.second != (v.first.second + 2)) {
- if (marked[v.first.first - 1][ v.first.second + 2][v.second.first][v.second.second] == 0) {
- marked[v.first.first - 1][v.first.second + 2][v.second.first][v.second.second] = 1;
- dist[v.first.first - 1][v.first.second + 2][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first - 1][v.first.second + 2][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first - 1, v.first.second + 2}, v.second });
- }
- }
- if (v.first.first + 1 < 9 and v.first.second + 2 < 9 and v.second.first != (v.first.first + 1) and v.second.second != (v.first.second + 2)) {
- if (marked[v.first.first + 1][v.first.second + 2][v.second.first][v.second.second] == 0) {
- marked[v.first.first + 1][v.first.second + 2][v.second.first][v.second.second] = 1;
- dist[v.first.first + 1][v.first.second + 2][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first + 1][v.first.second + 2][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first + 1, v.first.second + 2}, v.second });
- }
- }
- if (v.first.first - 1 > 0 and v.first.second - 2 > 0 and v.second.first != (v.first.first - 1) and v.second.second != (v.first.second - 2)) {
- if (marked[v.first.first - 1][v.first.second - 2][v.second.first][v.second.second] == 0) {
- marked[v.first.first - 1][v.first.second - 2][v.second.first][v.second.second] = 1;
- dist[v.first.first - 1][v.first.second - 2][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first - 1][v.first.second - 2][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first - 1, v.first.second - 2}, v.second });
- }
- }
- if (v.first.first + 1 < 9 and v.first.second - 2 > 0 and v.second.first != (v.first.first + 1) and (v.second.second != v.first.second - 2)) {
- if (marked[v.first.first + 1][v.first.second - 2][v.second.first][v.second.second] == 0) {
- marked[v.first.first + 1][v.first.second - 2][v.second.first][v.second.second] = 1;
- dist[v.first.first + 1][v.first.second - 2][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first + 1][v.first.second - 2][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first + 1, v.first.second - 2}, v.second });
- }
- }
- //
- if (v.first.first + 2 < 9 and v.first.second + 1 < 9 and v.second.first != (v.first.first + 2) and v.second.second != (v.first.second + 1)) {
- if (marked[v.first.first + 2][v.first.second + 1][v.second.first][v.second.second] == 0) {
- marked[v.first.first + 2][v.first.second + 1][v.second.first][v.second.second] = 1;
- dist[v.first.first + 2][v.first.second + 1][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first + 2][v.first.second + 1][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first + 2, v.first.second + 1}, v.second });
- }
- }
- if (v.first.first + 2 < 9 and v.first.second - 1 > 0 and v.second.first != (v.first.first + 2) and v.second.second != (v.first.second - 1)) {
- if (marked[v.first.first + 2][v.first.second - 1][v.second.first][v.second.second] == 0) {
- marked[v.first.first + 2][v.first.second - 1][v.second.first][v.second.second] = 1;
- dist[v.first.first + 2][v.first.second - 1][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first + 2][v.first.second - 1][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first + 2, v.first.second - 1}, v.second });
- }
- }
- if (v.first.first - 2 > 0 and v.first.second + 1 < 9 and v.second.first != (v.first.first - 2) and v.second.second != (v.first.second + 1)) {
- if (marked[v.first.first - 2][v.first.second + 1][v.second.first][v.second.second] == 0) {
- marked[v.first.first - 2][v.first.second + 1][v.second.first][v.second.second] = 1;
- dist[v.first.first - 2][v.first.second + 1][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first - 2][v.first.second + 1][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first - 2, v.first.second + 1}, v.second });
- }
- }
- if (v.first.first - 2 > 0 and v.first.second - 1 > 0 and (v.second.first != v.first.first - 2) and v.second.second != (v.first.second - 1)) {
- if (marked[v.first.first - 2][v.first.second - 1][v.second.first][v.second.second] == 0) {
- marked[v.first.first - 2][v.first.second - 1][v.second.first][v.second.second] = 1;
- dist[v.first.first - 2][v.first.second - 1][v.second.first][v.second.second] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first - 2][v.first.second - 1][v.second.first][v.second.second] = v;
- queue.push({ {v.first.first - 2, v.first.second - 1}, v.second });
- }
- }
- /// SECOND
- if (v.second.first - 1 > 0 and v.second.second + 2 < 9 and v.first.first != (v.second.first - 1) and v.first.second != (v.second.second + 2)) {
- if (marked[v.first.first][v.first.second][v.second.first - 1][v.second.second + 2] == 0) {
- marked[v.first.first][v.first.second][v.second.first - 1][v.second.second + 2] = 1;
- dist[v.first.first][v.first.second][v.second.first - 1][v.second.second + 2] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first - 1][v.second.second + 2] = v;
- queue.push({ v.first ,{v.second.first - 1, v.second.second + 2} });
- }
- }
- if (v.second.first + 1 < 9 and v.second.second + 2 < 9 and v.first.first != (v.second.first + 1) and v.first.second != (v.second.second + 2)) {
- if (marked[v.first.first][v.first.second][v.second.first + 1][v.second.second + 2] == 0) {
- marked[v.first.first][v.first.second][v.second.first + 1][v.second.second + 2] = 1;
- dist[v.first.first][v.first.second][v.second.first + 1][v.second.second + 2] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first + 1][v.second.second + 2] = v;
- queue.push({ v.first ,{v.second.first + 1, v.second.second + 2} });
- }
- }
- if (v.second.first - 1 > 0 and v.second.second - 2 > 0 and v.first.first != (v.second.first - 1) and v.first.second != (v.second.second - 2)) {
- if (marked[v.first.first][v.first.second][v.second.first - 1][v.second.second - 2] == 0) {
- marked[v.first.first][v.first.second][v.second.first - 1][v.second.second - 2] = 1;
- dist[v.first.first][v.first.second][v.second.first - 1][v.second.second - 2] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first - 1][v.second.second - 2] = v;
- queue.push({ v.first ,{v.second.first - 1, v.second.second - 2} });
- }
- }
- if (v.second.first + 1 < 9 and v.second.second - 2 > 0 and v.first.first != (v.second.first + 1) and v.first.second != (v.second.second - 2)) {
- if (marked[v.first.first][v.first.second][v.second.first + 1][v.second.second - 2] == 0) {
- marked[v.first.first][v.first.second][v.second.first + 1][v.second.second - 2] = 1;
- dist[v.first.first][v.first.second][v.second.first + 1][v.second.second - 2] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first + 1][v.second.second - 2] = v;
- queue.push({ v.first ,{v.second.first + 1, v.second.second - 2} });
- }
- }
- //
- if (v.second.first + 2 < 9 and v.second.second + 1 < 9 and v.first.first != (v.second.first + 2) and v.first.second != (v.second.second + 1)) {
- if (marked[v.first.first][v.first.second][v.second.first + 2][v.second.second + 1] == 0) {
- marked[v.first.first][v.first.second][v.second.first + 2][v.second.second + 1] = 1;
- dist[v.first.first][v.first.second][v.second.first + 2][v.second.second + 1] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first + 2][v.second.second + 1] = v;
- queue.push({ v.first ,{v.second.first + 2, v.second.second + 1} });
- }
- }
- if (v.second.first + 2 < 9 and v.second.second - 1 > 0 and (v.second.first + 2) != (v.first.first) and (v.second.second - 1) != v.first.second) {
- if (marked[v.first.first][v.first.second][v.second.first + 2][v.second.second - 1] == 0) {
- marked[v.first.first][v.first.second][v.second.first + 2][v.second.second - 1] = 1;
- dist[v.first.first][v.first.second][v.second.first + 2][v.second.second - 1] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first + 2][v.second.second - 1] = v;
- queue.push({ v.first ,{v.second.first + 2, v.second.second - 1} });
- }
- }
- if (v.second.first - 2 > 0 and v.second.second + 1 < 9 and (v.second.first - 2) != (v.first.first) and (v.second.second + 1) != v.first.second) {
- if (marked[v.first.first][v.first.second][v.second.first - 2][v.second.second + 1] == 0) {
- marked[v.first.first][v.first.second][v.second.first - 2][v.second.second + 1] = 1;
- dist[v.first.first][v.first.second][v.second.first - 2][v.second.second + 1] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first - 2][v.second.second + 1] = v;
- queue.push({ v.first ,{v.second.first - 2, v.second.second + 1} });
- }
- }
- if (v.second.first - 2 > 0 and v.second.second - 1 > 0 and (v.second.first - 2) != (v.first.first) and (v.second.second - 1) != v.first.second) {
- if (marked[v.first.first][v.first.second][v.second.first - 2][v.second.second - 1] == 0) {
- marked[v.first.first][v.first.second][v.second.first - 2][v.second.second - 1] = 1;
- dist[v.first.first][v.first.second][v.second.first - 2][v.second.second - 1] = dist[v.first.first][v.first.second][v.second.first][v.second.second] + 1;
- back[v.first.first][v.first.second][v.second.first - 2][v.second.second - 1] = v;
- queue.push({ v.first ,{v.second.first - 2, v.second.second - 1} });
- }
- }
- queue.pop();
- }
- pair<pair<int, int>, pair<int, int>> x = end;
- vector<pair<int, pair<char, int>>> ans;
- for (int i = 0; i < dist[end.first.first][end.first.second][end.second.first][end.second.second]; i++) {
- pair<pair<int, int>, pair<int, int>> y = back[x.first.first][x.first.second][x.second.first][x.second.second];
- if (y.first != x.first) {
- char symb = 'a' + y.first.first;
- ans.push_back({ 1, {x.first.first + 'a' - 1, x.first.second} });
- }
- else {
- ans.push_back({ 2, {x.second.first + 'a' - 1, x.second.second} });
- }
- x = y;
- }
- for (int i = ans.size() - 1; i >= 0; i--) {
- cout << ans[i].first << ' ' << ans[i].second.first << ans[i].second.second << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement