Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <math.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <string>
  7. #include <numeric>
  8. #include <set>
  9. #include <queue>
  10. #define int long long
  11. using namespace std;
  12.  
  13. int dx[8] = { 2, 2, -2, -2, 1, 1, -1, -1 };
  14. int dy[8] = { 1, -1, 1, -1, 2, -2, 2, -2 };
  15.  
  16.  
  17.  
  18.  
  19. int x(char x1) {
  20.     if (x1 == 'a') {
  21.         return 0;
  22.     }
  23.     if (x1 == 'b') {
  24.         return 1;
  25.     }
  26.     if (x1 == 'c') {
  27.         return 2;
  28.     }
  29.     if (x1 == 'd') {
  30.         return 3;
  31.     }
  32.     if (x1 == 'e') {
  33.         return 4;
  34.     }
  35.     if (x1 == 'f') {
  36.         return 5;
  37.     }
  38.     if (x1 == 'g') {
  39.         return 6;
  40.     }
  41.     if (x1 == 'h') {
  42.         return 7;
  43.     }
  44. }
  45.  
  46. int y(char y1) {
  47.     if (y1 == '1') {
  48.         return 0;
  49.     }
  50.     if (y1 == '2') {
  51.         return 1;
  52.     }
  53.     if (y1 == '3') {
  54.         return 2;
  55.     }
  56.     if (y1 == '4') {
  57.         return 3;
  58.     }
  59.     if (y1 == '5') {
  60.         return 4;
  61.     }
  62.     if (y1 == '6') {
  63.         return 5;
  64.     }
  65.     if (y1 == '7') {
  66.         return 6;
  67.     }
  68.     if (y1 == '8') {
  69.         return 7;
  70.     }
  71. }
  72.  
  73. bool truth(int x, int y) {
  74.     return x >= 0 && x < 8 && y >= 0 && y < 8;
  75. }
  76.  
  77. void solve() {
  78.     string s1, s2;
  79.     cin >> s1 >> s2;
  80.     int x1, y1, x2, y2;
  81.     x1 = x(s1[0]); x2 = x(s2[0]);
  82.     y1 = y(s1[1]); y2 = y(s2[1]);
  83.    
  84.     vector<vector<int>> dist(8, vector<int>(8));
  85.     for (int i = 0; i < 8; i++) {
  86.         for (int j = 0; j < 8; j++) {
  87.             dist[i][j] = 1000000000;
  88.         }
  89.     }
  90.     dist[x1][y1] = 0;
  91.     vector<vector<pair<int, int>>> pred(8, vector<pair<int, int>>(8));
  92.     queue<pair<int, int>> q;
  93.     q.push({ x1, y1 });
  94.     while (!q.empty()) {
  95.         pair<int, int> cor = q.front();
  96.         q.pop();
  97.         int x = cor.first;
  98.         int y = cor.second;
  99.         for (int i = 0; i < 8; i++) {
  100.             int nowx = x + dx[i]; int nowy = y + dy[i];
  101.             if (truth(nowx, nowy) && dist[nowx][nowy] == 1000000000) {
  102.                 q.push({ nowx, nowy });
  103.                 dist[nowx][nowy] = dist[x][y] + 1;
  104.                 pred[nowx][nowy] = { x, y };
  105.             }
  106.         }
  107.     }
  108.     vector<pair<int, int>> ans;
  109.     int nowx = x2, nowy = y2;
  110.     while (x2 != x1 && y2 != y1) {
  111.         ans.push_back({ nowx, nowy });
  112.         nowx = pred[x2][y2].first; nowy = pred[x2][y2].second;
  113.         x2 = nowx; y2 = nowy;
  114.     }
  115.     ans.push_back({ x1, y1 });
  116.     for (int i = ans.size() - 1; i >= 0; i--) {
  117.         int ansx = ans[i].first, ansy = ans[i].second;
  118.         if (ansx == 0)
  119.             cout << "a";
  120.         else if (ansx == 1)
  121.             cout << "b";
  122.         else if (ansx == 2)
  123.             cout << "c";
  124.         else if (ansx == 3)
  125.             cout << "d";
  126.         else if (ansx == 4)
  127.             cout << "e";
  128.         else if (ansx == 5)
  129.             cout << "f";
  130.         else if (ansx == 6)
  131.             cout << "g";
  132.         else if (ansx == 7)
  133.             cout << "h";
  134.         cout << ansy + 1 << "\n";
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. signed main() {
  141.     //freopen("mushrooms.in", "r", stdin);
  142.     //freopen("mushrooms.out", "w", stdout);
  143.     cin.tie(0);
  144.     cout.tie(0);
  145.     solve();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement