Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <stdio.h>
- #include <queue>
- #include <stack>
- using namespace std;
- class step {
- public:
- long x, y, len;
- void print() {
- printf("%ld %ld\n", x, y);
- }
- };
- void stepprint(step *steps, long len) {
- if (len > -1)
- {
- stepprint(steps, steps[len].len);
- steps[len].print();
- }
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- long xarr[8] = { 1,2,2,1,-1,-2,-2,-1 }, yarr[8] = { 2,1,-1,-2,-2,-1,1,2 };
- long field[21][21]{}, k = 0, var = 0, check = 0, cnt = 0, ptr = 1;
- bool cont = false;
- step steps[401];
- long n, m, x2, y2;
- scanf("%ld%ld%ld%ld%ld%ld", &n, &m, &steps[0].x, &steps[0].y, &x2, &y2);
- steps[0].len = -1;
- do {
- cont = false;
- for (int i = k; i <= var && !check; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- long x = steps[i].x + xarr[j];
- long y = steps[i].y + yarr[j];
- if (field[x][y] != 0 || x < 1 || x > n || y < 1 || y > m)
- {
- continue;
- }
- else
- {
- field[x][y]++;
- cont = true;
- steps[ptr].x = x; steps[ptr].y = y; steps[ptr].len = i;
- ptr++;
- if (x == x2 && y == y2)
- {
- check = ptr - 1;
- break;
- }
- }
- }
- }
- cnt++;
- k = var + 1;
- var = ptr - 1;
- } while (!check && cont);
- if (check)
- {
- printf("%ld\n", cnt);
- stepprint(steps, check);
- }
- else
- {
- printf("-1");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement