Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <vector>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <stdio.h>
  6. #include <queue>
  7. #include <stack>
  8.  
  9. using namespace std;
  10.  
  11. class step {
  12. public:
  13.     long x, y, len;
  14.     void print() {
  15.         printf("%ld %ld\n", x, y);
  16.     }
  17. };
  18.  
  19. void stepprint(step *steps, long len) {
  20.     if (len > -1)
  21.     {
  22.         stepprint(steps, steps[len].len);
  23.         steps[len].print();
  24.     }
  25. }
  26.  
  27. int main() {
  28. freopen("input.txt", "r", stdin);
  29. freopen("output.txt", "w", stdout);
  30.     long xarr[8] = { 1,2,2,1,-1,-2,-2,-1 }, yarr[8] = { 2,1,-1,-2,-2,-1,1,2 };
  31.     long field[21][21]{}, k = 0, var = 0, check = 0, cnt = 0, ptr = 1;
  32.     bool cont = false;
  33.     step steps[401];
  34.     long n, m, x2, y2;
  35.     scanf("%ld%ld%ld%ld%ld%ld", &n, &m, &steps[0].x, &steps[0].y, &x2, &y2);
  36.     steps[0].len = -1;
  37.  
  38.     do {
  39.         cont = false;
  40.         for (int i = k; i <= var && !check; i++)
  41.         {
  42.             for (int j = 0; j < 8; j++)
  43.             {
  44.                 long x = steps[i].x + xarr[j];
  45.                 long y = steps[i].y + yarr[j];
  46.                 if (field[x][y] != 0 || x < 1 || x > n || y < 1 || y > m)
  47.                 {
  48.                     continue;
  49.                 }
  50.                 else
  51.                 {
  52.                     field[x][y]++;
  53.                     cont = true;
  54.                     steps[ptr].x = x; steps[ptr].y = y; steps[ptr].len = i;
  55.                     ptr++;
  56.                     if (x == x2 && y == y2)
  57.                     {
  58.                         check = ptr - 1;
  59.                         break;
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         cnt++;
  65.         k = var + 1;
  66.         var = ptr - 1;
  67.     } while (!check && cont);
  68.  
  69.     if (check)
  70.     {
  71.         printf("%ld\n", cnt);
  72.         stepprint(steps, check);
  73.     }
  74.     else
  75.     {
  76.         printf("-1");
  77.     }
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement