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".
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.
The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise.
2 4 2 R 1 1 B 1 5
YES
2 4 2 R 3 3 B 1 5
NO
Picture for the first sample:

Picture for the second sample:

题目大意:
已知一共有N个黑色的棋子,对应白色的棋子的位子也是已知的。
已知黑色棋子一共有三种:
B:只能攻击对角线上的敌人。
R:只能攻击其同行同列的敌人。
Q:能够攻击对角线和同行同列上的敌人。
每个棋子都不能跨越另外一个棋子去吃其他棋子。
问是否白色棋子已经被将军了。
思路:
1、预处理出所有黑色棋子和白色棋子的距离。然后我们将所有棋子按照距离从小到大排序。
2、然后维护其白色棋子的周围八个方向遇到的第一个棋子能否将当前白色棋子将军即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
char s[5];
int x,y;
long long int dis;
}a[500050];
int x,y;
int vis[8];
int cmp(node a,node b)
{
return a.dis<b.dis;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&x,&y);
for(int i=0;i<n;i++)
{
scanf("%s%d%d",a[i].s,&a[i].x,&a[i].y);
long long int tmp1=a[i].x;
tmp1-=x;
tmp1*=tmp1;
long long int tmp2=a[i].y;
tmp2-=y;
tmp2*=tmp2;
a[i].dis=tmp1+tmp2;
}
int flag=0;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
if(a[i].x==x)
{
if(a[i].y>y)
{
if(vis[0]==1)continue;
vis[0]=1;
if(a[i].s[0]=='R'||a[i].s[0]=='Q')flag=1;
}
else
{
if(vis[1]==1)continue;
vis[1]=1;
if(a[i].s[0]=='R'||a[i].s[0]=='Q')flag=1;
}
}
if(a[i].y==y)
{
if(a[i].x>x)
{
if(vis[2]==1)continue;
vis[2]=1;
if(a[i].s[0]=='R'||a[i].s[0]=='Q')flag=1;
}
else
{
if(vis[3]==1)continue;
vis[3]=1;
if(a[i].s[0]=='R'||a[i].s[0]=='Q')flag=1;
}
}
if(abs(a[i].x-x)==abs(a[i].y-y))
{
if(a[i].x>x&&a[i].y>y)
{
if(vis[4]==1)continue;
vis[4]=1;
if(a[i].s[0]=='Q'||a[i].s[0]=='B')flag=1;
}
if(a[i].x>x&&a[i].y<y)
{
if(vis[5]==1)continue;
vis[5]=1;
if(a[i].s[0]=='Q'||a[i].s[0]=='B')flag=1;
}
if(a[i].x<x&&a[i].y>y)
{
if(vis[6]==1)continue;
vis[6]=1;
if(a[i].s[0]=='Q'||a[i].s[0]=='B')flag=1;
}
if(a[i].x<x&&a[i].y<y)
{
if(vis[7]==1)continue;
vis[7]=1;
if(a[i].s[0]=='Q'||a[i].s[0]=='B')flag=1;
}
}
}
if(flag==1)printf("YES\n");
else printf("NO\n");
}
}