Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <math.h>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <numeric>
- #include <set>
- #include <queue>
- #define int long long
- using namespace std;
- int dx[8] = { 2, 2, -2, -2, 1, 1, -1, -1 };
- int dy[8] = { 1, -1, 1, -1, 2, -2, 2, -2 };
- int x(char x1) {
- if (x1 == 'a') {
- return 0;
- }
- if (x1 == 'b') {
- return 1;
- }
- if (x1 == 'c') {
- return 2;
- }
- if (x1 == 'd') {
- return 3;
- }
- if (x1 == 'e') {
- return 4;
- }
- if (x1 == 'f') {
- return 5;
- }
- if (x1 == 'g') {
- return 6;
- }
- if (x1 == 'h') {
- return 7;
- }
- }
- int y(char y1) {
- if (y1 == '1') {
- return 0;
- }
- if (y1 == '2') {
- return 1;
- }
- if (y1 == '3') {
- return 2;
- }
- if (y1 == '4') {
- return 3;
- }
- if (y1 == '5') {
- return 4;
- }
- if (y1 == '6') {
- return 5;
- }
- if (y1 == '7') {
- return 6;
- }
- if (y1 == '8') {
- return 7;
- }
- }
- bool truth(int x, int y) {
- return x >= 0 && x < 8 && y >= 0 && y < 8;
- }
- void solve() {
- string s1, s2;
- cin >> s1 >> s2;
- int x1, y1, x2, y2;
- x1 = x(s1[0]); x2 = x(s2[0]);
- y1 = y(s1[1]); y2 = y(s2[1]);
- vector<vector<int>> dist(8, vector<int>(8));
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 8; j++) {
- dist[i][j] = 1000000000;
- }
- }
- dist[x1][y1] = 0;
- vector<vector<pair<int, int>>> pred(8, vector<pair<int, int>>(8));
- queue<pair<int, int>> q;
- q.push({ x1, y1 });
- while (!q.empty()) {
- pair<int, int> cor = q.front();
- q.pop();
- int x = cor.first;
- int y = cor.second;
- for (int i = 0; i < 8; i++) {
- int nowx = x + dx[i]; int nowy = y + dy[i];
- if (truth(nowx, nowy) && dist[nowx][nowy] == 1000000000) {
- q.push({ nowx, nowy });
- dist[nowx][nowy] = dist[x][y] + 1;
- pred[nowx][nowy] = { x, y };
- }
- }
- }
- vector<pair<int, int>> ans;
- int nowx = x2, nowy = y2;
- while (x2 != x1 && y2 != y1) {
- ans.push_back({ nowx, nowy });
- nowx = pred[x2][y2].first; nowy = pred[x2][y2].second;
- x2 = nowx; y2 = nowy;
- }
- ans.push_back({ x1, y1 });
- for (int i = ans.size() - 1; i >= 0; i--) {
- int ansx = ans[i].first, ansy = ans[i].second;
- if (ansx == 0)
- cout << "a";
- else if (ansx == 1)
- cout << "b";
- else if (ansx == 2)
- cout << "c";
- else if (ansx == 3)
- cout << "d";
- else if (ansx == 4)
- cout << "e";
- else if (ansx == 5)
- cout << "f";
- else if (ansx == 6)
- cout << "g";
- else if (ansx == 7)
- cout << "h";
- cout << ansy + 1 << "\n";
- }
- }
- signed main() {
- //freopen("mushrooms.in", "r", stdin);
- //freopen("mushrooms.out", "w", stdout);
- cin.tie(0);
- cout.tie(0);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement