-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkingescape.cpp
More file actions
38 lines (34 loc) · 1.63 KB
/
Copy pathkingescape.cpp
File metadata and controls
38 lines (34 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Problem Name: King Escape
Link to problem: https://codeforces.com/contest/1033/problem/A
*/
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<bool>>& board, int q_row, int q_col, int kw_row, int kw_col, int i, int j, bool& can_escape, int n){
if (can_escape) return;
if (i < 1 || i > n || j < 1 || j > n) return;
if (board[i - 1][j - 1]) return;
if (i == q_row || j == q_col || abs(i - q_row) == abs(j - q_col)) return;
board[i - 1][j - 1] = true;
if (i == kw_row && j == kw_col) {
can_escape = true;
return;
}
board[i - 1][j - 1] = true;
dfs(board, q_row, q_col, kw_row, kw_col, i + 1, j, can_escape, n);
dfs(board, q_row, q_col, kw_row, kw_col, i - 1, j, can_escape, n);
dfs(board, q_row, q_col, kw_row, kw_col, i, j + 1, can_escape, n);
dfs(board, q_row, q_col, kw_row, kw_col, i, j - 1, can_escape, n);
dfs(board, q_row, q_col, kw_row, kw_col, i + 1, j + 1, can_escape, n);
dfs(board, q_row, q_col, kw_row, kw_col, i - 1, j + 1, can_escape, n);
dfs(board, q_row, q_col, kw_row, kw_col, i + 1, j - 1, can_escape, n);
dfs(board, q_row, q_col, kw_row, kw_col, i - 1, j - 1, can_escape, n);
}
int main() {
int n, queen_row, queen_col, king_row, king_col, king_wanted_row, king_wanted_col; cin >> n >> queen_row >> queen_col >> king_row >> king_col >> king_wanted_row >> king_wanted_col;
bool can_escape = false;
vector<vector<bool>> board(n, vector<bool>(n, false));
dfs(board, queen_row, queen_col, king_wanted_row, king_wanted_col, king_row, king_col, can_escape, n);
if (can_escape) cout << "YES" << endl;
else cout << "NO" << endl;
}