Advertisement
limimage

KONI

Feb 8th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7. using pii = pair<int, int>;
  8.  
  9. constexpr int N = 100;
  10.  
  11. tuple<bool, char, char> par[2][N][N];
  12. bool used[2][N][N];
  13. pii d[] = {{2, 1}, {2, -1}, {1, 2}, {-1, 2}, {-2, -1}, {-2, 1}, {-1, -2}, {1, -2}};
  14. int st1, st2, fn2, fn1;
  15. queue<tuple<bool, char, char>> q;
  16. vector<pair<bool, char>> ans;
  17.  
  18. bool check(char x, char y) {
  19. return x >= 0 && x < 8 && y >= 0 && y < 8;
  20. }
  21.  
  22. void bfs() {
  23. q.emplace(0, st1, st2);
  24. q.emplace(1, st1, st2);
  25. used[0][st1][st2] = 1;
  26. used[1][st1][st2] = 1;
  27. while(q.size()) {
  28. auto [t, p, w] = q.front();
  29. q.pop();
  30. char px = p / 8, py = p % 8;
  31. char wx = w / 8, wy = w % 8;
  32. if (t) {
  33. for (auto [dx, dy]: d) {
  34. if (check(wx + dx, wy + dy)) {
  35. char np = px * 8 + py;
  36. char nw = (wx + dx) * 8 + wy + dy;
  37. if (np != nw) {
  38. if (!used[1][np][nw]) {
  39. used[1][np][nw] = 1;
  40. q.emplace(1, np, nw);
  41. par[1][np][nw] = {t, p, w};
  42. }
  43. if (!used[0][np][nw]) {
  44. used[0][np][nw] = 1;
  45. q.emplace(0, np, nw);
  46. par[0][np][nw] = {t, p, w};
  47. }
  48. }
  49. }
  50. }
  51. }
  52. else {
  53. for (auto [dx, dy]: d) {
  54. if (check(px + dx, py + dy)) {
  55. char np = (px + dx) * 8 + py + dy;
  56. char nw = wx * 8 + wy;
  57. if (np != nw) {
  58. if (!used[1][np][nw]) {
  59. used[1][np][nw] = 1;
  60. q.emplace(1, np, nw);
  61. par[1][np][nw] = {t, p, w};
  62. }
  63. if (!used[0][np][nw]) {
  64. used[0][np][nw] = 1;
  65. q.emplace(0, np, nw);
  66. par[0][np][nw] = {t, p, w};
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74.  
  75. char c;
  76. int get_pos() {
  77. int ans = 0;
  78. cin >> c;
  79. ans = c - 'a';
  80. ans *= 8;
  81. cin >> c;
  82. ans += c - '1';
  83. return ans;
  84. }
  85.  
  86. void print(char x) {
  87. char f = x / 8;
  88. char s = x % 8;
  89. f += 'a';
  90. s += '1';
  91. cout << f << s;
  92. }
  93.  
  94. void Solve() {
  95. st1 = get_pos();
  96. st2 = get_pos();
  97. fn1 = get_pos();
  98. fn2 = get_pos();
  99. bfs();
  100. for (int i = 0, x = fn1, y = fn2; x != st1 || y != st2; ) {
  101. auto [ni, nx, ny] = par[i][x][y];
  102. if (ni)
  103. ans.emplace_back(1, y);
  104. else
  105. ans.emplace_back(0, x);
  106. i = ni;
  107. x = nx;
  108. y = ny;
  109. }
  110. reverse(ans.begin(), ans.end());
  111. for (auto [t, x]: ans) {
  112. cout << t + 1 << " ";
  113. print(x);
  114. cout << endl;
  115. }
  116. }
  117.  
  118. int main() {
  119. ios::sync_with_stdio(false);
  120. cin.tie(nullptr);
  121. cout.tie(nullptr);
  122. //auto start = chrono::high_resolution_clock::now();
  123. Solve();
  124. // auto end = chrono::high_resolution_clock::now();
  125. // cout << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement