Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- bool isValid(int x, int y, int K, int N) {
- return x >= 0 && y >= 0 && x < K && y < N;
- }
- bool canReach(int K, int N, int j, int i, int m, int s) {
- vector<pair<int, int>> moves = {{-2, -1},
- {-2, 1},
- {-1, -2},
- {-1, 2},
- {1, -2},
- {1, 2},
- {2, -1},
- {2, 1}};
- vector<vector<bool>> visited(K, vector<bool>(N, false));
- queue<pair<int, int>> q;
- q.emplace(j - 1, i - 1);
- visited[j - 1][i - 1] = true;
- while (!q.empty()) {
- auto [x, y] = q.front();
- q.pop();
- if (x == m - 1 && y == s - 1) return true;
- for (auto &move: moves) {
- int nx = x + move.first, ny = y + move.second;
- if (isValid(nx, ny, K, N) && !visited[nx][ny]) {
- visited[nx][ny] = true;
- q.emplace(nx, ny);
- }
- }
- }
- return false;
- }
- int main() {
- int K, N, j, i, m, s;
- cin >> K >> N >> j >> i >> m >> s;
- if (canReach(K, N, j, i, m, s))
- cout << "YES" << endl;
- else
- cout << "NO" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement