Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 22;
- int n;
- int f1, s1;
- int f2, s2;
- int used[N][N];
- int dist[N][N];
- int main() {
- cin >> n;
- cin >> f1 >> s1;
- cin >> f2 >> s2;
- queue < pair <int, int> > q;
- used[f1][s1] = 1;
- q.push({f1, s1});
- while (!q.empty()) {
- int x = q.front().first, y = q.front().second;
- q.pop();
- if (x - 1 >= 1 && y - 2 >= 1 && !used[x - 1][y - 2]) {
- used[x - 1][y - 2] = 1;
- dist[x - 1][y - 2] = dist[x][y] + 1;
- q.push({x - 1, y - 2});
- }
- if (x - 1 >= 1 && y + 2 <= n && !used[x - 1][y + 2]) {
- used[x - 1][y + 2] = 1;
- dist[x - 1][y + 2] = dist[x][y] + 1;
- q.push({x - 1, y + 2});
- }
- if (x + 1 <= n && y - 2 >= 1 && !used[x + 1][y - 2]) {
- used[x + 1][y - 2] = 1;
- dist[x + 1][y - 2] = dist[x][y] + 1;
- q.push({x + 1, y - 2});
- }
- if (x + 1 <= n && y + 2 <= n && !used[x + 1][y + 2]) {
- used[x + 1][y + 2] = 1;
- dist[x + 1][y + 2] = dist[x][y] + 1;
- q.push({x + 1, y + 2});
- }
- if (x - 2 >= 1 && y - 1 >= 1 && !used[x - 2][y - 1]) {
- used[x - 2][y - 1] = 1;
- dist[x - 2][y - 1] = dist[x][y] + 1;
- q.push({x - 2, y - 1});
- }
- if (x - 2 >= 1 && y + 1 <= n && !used[x - 2][y + 1]) {
- used[x - 2][y + 1] = 1;
- dist[x - 2][y + 1] = dist[x][y] + 1;
- q.push({x - 2, y + 1});
- }
- if (x + 2 <= n && y - 1 >= 1 && !used[x + 2][y - 1]) {
- used[x + 2][y - 1] = 1;
- dist[x + 2][y - 1] = dist[x][y] + 1;
- q.push({x + 2, y - 1});
- }
- if (x + 2 <= n && y + 1 <= n && !used[x + 2][y + 1]) {
- used[x + 2][y + 1] = 1;
- dist[x + 2][y + 1] = dist[x][y] + 1;
- q.push({x + 2, y + 1});
- }
- }
- cout << dist[f2][s2] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement