Advertisement
kolbka_

Untitled

Mar 4th, 2022
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1.  
  2. #include <unordered_map>
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6. #include <set>
  7. #include <queue>
  8. #include "optimization.h"
  9.  
  10. using namespace std;
  11. vector<vector<int>> matrix;
  12. vector<bool> used;
  13. int answer = 0;
  14. int n, m;
  15.  
  16. string f, s;
  17. string f_o, s_o;
  18. vector<pair<int, int>> moves = {{ -1, 2 },
  19.                                 { 1, 2 },
  20.  
  21.                                 { 2, 1 },
  22.                                 { 2, -1 },
  23.  
  24.                                 { 1, -2 },
  25.                                 { -1, -2 },
  26.  
  27.                                 { -2, -1 },
  28.                                 { -2, 1 },};
  29. unordered_map<string, bool> used1, used2;
  30.  
  31. unordered_map<string, string> was;
  32.  
  33. auto id(string a, string b) {
  34.     return was[a + b];
  35. }
  36.  
  37. vector<string>result;
  38. void bfs() {
  39.     queue<pair<string, string>> q;
  40.     q.push({f, s});
  41.     while (!q.empty()) {
  42.         auto[m1_s, m2_s] = q.front();
  43.         q.pop();
  44.         string m1 = m1_s;
  45.         string m2 = m2_s;
  46.         if (m1 == f_o && m2 == s_o) {
  47.             auto y = m1+m2;
  48.             while (y != f+s) {
  49.                 auto t = was[y];
  50.                 if (t.substr(0,2) == y.substr(0,2)){
  51.                     result.push_back("2 " + y.substr(2));
  52.                 }
  53.                 else{
  54.  
  55.                     result.push_back("1 "+ y.substr(0,2));
  56.                 }
  57.                 y = t;
  58.             }
  59.             return;
  60.         }
  61.         for (auto[x, y]: moves) {
  62.             m1 = m1_s; m2 = m2_s;
  63.             m1[0] += x; m1[1] += y;
  64.             if (m1 != m2 && m1[0] <= 'h' && m1[0] >= 'a' && m1[1] <= '8' && m1[1] >= '1' && !used1[m1 + m2]) {
  65.                 was[m1 + m2] = m1_s+m2_s;
  66.                 q.push({m1, m2});
  67.                 used1[m1+m2] = true;
  68.             }
  69.             m1 = m1_s;
  70.             m2[0] += x; m2[1] += y;
  71.             if (m1 != m2 && m2[0] <= 'h' && m2[0] >= 'a' && m2[1] <= '8' && m2[1] >= '1' && !used1[m1+m2]) {
  72.                 was[m1 + m2] = m1_s + m2_s;
  73.                 q.push({m1, m2});
  74.                 used1[m1+m2] = true;
  75.             }
  76.             m2 = m2_s;
  77.         }
  78.     }
  79. }
  80.  
  81.  
  82. int main() {
  83.     cin >> f >> s;
  84.     cin >> f_o >> s_o;
  85.     bfs();
  86.     for (int i = result.size()-1;i >= 0;i--){
  87.         cout << result[i] << '\n';
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement