Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.
The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help.
Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move.
Help Anton and write the program that for the given position determines whether the white king is in check.
Remainder, on how do chess pieces move:
Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells.
Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells.
Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap".
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of black pieces.
The second line contains two integers x0 and y0 ( - 109 ≤ x0, y0 ≤ 109) — coordinates of the white king.
Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position.
Output
The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise.
Example
Input
2
4 2
R 1 1
B 1 5
Output
YES
Input
2
4 2
R 3 3
B 1 5
Output
NO
Note
Picture for the first sample:
White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is " YES".
Picture for the second sample:
Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is " NO".
一道模拟题,情况很多,需要仔细。策略是按黑棋与国王的距离为关键值,值小的优先级高,压入优先队列。于是优先检查离国王近的黑棋是否可将军。若国王的八个方向都被堵死或被将军,可以中断判断,输出结果。
#include
#include
#include
#include
#include
#include
#define TEST
using namespace std;
long long n, king[4], x, y;// king: [0] horizontal; [1] vertical; [2] main diagonal; [3] counter diagonal;
bool ckmt[4][2];//国王的八向能否被将军
// [0][0] left; [0][1] right; [1][0] up; [1][1] down; [2][0] upper left; [2][1] upper right; [3][0] lower right; [3][1] lower left;
struct chess_piece{
long long x, y, md, cd;
char type;
chess_piece(int a, int b, char c):x(a), y(b), type(c), md(a+b), cd(a-b){}
bool operator < (const chess_piece &p)const{
return abs(x-king[0])+abs(y-king[1]) > abs(p.x-king[0])+abs(p.y-king[1]);
}
};
priority_queue q;
bool checkmate(){
while(!q.empty()){
int sz = q.size();
chess_piece temp = q.top();
q.pop();
long long vx = temp.x, vy = temp.y, vm = temp.md, cd = temp.cd;
char typ = temp.type;
switch (typ){
case 'R':
if(vm == king[2] || cd == king[3]){//on diagonal
if(vx < king[0] && vy > king[1])//upper left
ckmt[2][0] = false;
else if(vx > king[0] && vy > king[1])//upper right
ckmt[3][0] = false;
else if(vx < king[0] && vy < king[1])//lower left
ckmt[3][1] = false;
else if(vx > king[0] && vy < king[1])//lower right
ckmt[2][1] = false;
}
else{//on vertical or horizontal line
if(vx == king[0] && vy > king[1] && ckmt[1][0])//up
return true;
else if(vx == king[0] && vy < king[1] && ckmt[1][1])//down
return true;
else if(vx < king[0] && vy == king[1] && ckmt[0][0])//left
return true;
else if(vx > king[0] && vy == king[1] && ckmt[0][1])//right
return true;
}
break;
case 'B':
if(vm == king[2] || cd == king[3]){//on diagonal
if(vx < king[0] && vy > king[1] && ckmt[2][0])//upper left
return true;
else if(vx > king[0] && vy > king[1] && ckmt[3][0])//upper right
return true;
else if(vx < king[0] && vy < king[1] && ckmt[3][1])//lower left
return true;
else if(vx > king[0] && vy < king[1] && ckmt[2][1])//lower right
return true;
}
else{//on vertical or horizontal line
if(vx == king[0] && vy > king[1])//up
ckmt[1][0] = false;
else if(vx == king[0] && vy < king[1])//down
ckmt[1][1] = false;
else if(vx < king[0] && vy == king[1])//left
ckmt[0][0] = false;
else if(vx > king[0] && vy == king[1])//right
ckmt[0][1] = false;
}
break;
case 'Q':
if(vm == king[2] || cd == king[3]){//on diagonal
if(vx < king[0] && vy > king[1] && ckmt[2][0])//upper left
return true;
else if(vx > king[0] && vy > king[1] && ckmt[3][0])//upper right
return true;
else if(vx < king[0] && vy < king[1] && ckmt[3][1])//lower left
return true;
else if(vx > king[0] && vy < king[1] && ckmt[2][1])//lower right
return true;
}
else{//on vertical or horizontal line
if(vx == king[0] && vy > king[1] && ckmt[1][0])//up
return true;
else if(vx == king[0] && vy < king[1] && ckmt[1][1])//down
return true;
else if(vx < king[0] && vy == king[1] && ckmt[0][0])//left
return true;
else if(vx > king[0] && vy == king[1] && ckmt[0][1])//right
return true;
}
break;
}
int cnt = 0;
for(int z = 0; z < 4; z++)
for(int v = 0; v < 2; v++)
if(ckmt[z][v] == false)
cnt++;
if(cnt == 8)
return false;
}
return false;
}
int main(){
#ifdef TEST
freopen("test.txt", "r", stdin);
#endif // TEST
while(cin >> n){
cin >> king[0] >> king[1];
king[2] = king[0] + king[1], king[3] = king[0] - king[1];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 2; j++)
ckmt[i][j] = true;
while(!q.empty())
q.pop();
char typ;
getchar();
for(int i = 0; i < n; i++){
scanf("%c%d%d", &typ, &x, &y);
q.push(chess_piece(x,y,typ));
getchar();
}
if(checkmate())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}