Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <iomanip>
  6. #include <queue>
  7. #include <stack>
  8. #include <climits>
  9.  
  10. using namespace std;
  11.  
  12. struct horse {
  13.     long x, y, l;
  14. };
  15.  
  16. void print(horse* onewhitehorse, long v) {
  17.     if (v > -1)
  18.     {
  19.         print(onewhitehorse, onewhitehorse[v].l);
  20.         cout << onewhitehorse[v].x << " " << onewhitehorse[v].y<<endl;
  21.     }
  22. }
  23.  
  24. int main() {
  25.    // freopen("input.txt", "r", stdin);
  26.    // freopen("output.txt", "w", stdout);
  27.     int x[8] = { 1,2,2,1,-1,-2,-2,-1 };
  28.     int y[8] = { 2,1,-1,-2,-2,-1,1,2 };
  29.     long area[50][50]{};
  30.     bool end = false;
  31.     int s = 1, klv = 0, v = 0, u = 0, k = 0;
  32.     horse one[1000];
  33.     long n, m, x2, y2;
  34.     cin >> n >> m >> one[0].x >> one[0].y >> x2 >> y2;
  35.     one[0].l = -1;
  36.  
  37.     do {
  38.         end = false;
  39.         for (int i = k; v == false && i <= u; i++) {
  40.             for (int j = 0; j < 8; j++) {
  41.                 long y1 = one[i].y + y[j];
  42.                 long x1 = one[i].x + x[j];
  43.                 if (x1 < 1 || y1 < 1 || area[x1][y1] != 0 || x1 > n || y1 > m) continue;
  44.                 else{
  45.                     end = true;
  46.                     area[x1][y1]++;
  47.                     one[s].l = i;
  48.                     one[s].x = x1; one[s].y = y1;
  49.                     s++;
  50.                     if (x1 == x2 && y1 == y2){
  51.                         v = s - 1; break;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         klv++;
  57.         k = u + 1;
  58.         u = s - 1;
  59.     } while (v == false && end == true);
  60.  
  61.     if (v == true) {
  62.         cout << klv << endl;
  63.         print(one, v);
  64.     }
  65.     else cout << "-1";
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement