(CodeForces

来源:互联网 发布:淘宝店铺推广平台 编辑:程序博客网 时间:2024/05/22 05:02

(CodeForces - 734D)Anton and Chess

time limit per test:4 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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.

Examples

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”.

题目大意:在n*n的国际象棋棋盘上,告诉白棋的位置(x,y),和黑棋的位置,黑棋有三种:
‘B’:只能走斜线;
‘R’:只能走直线(水平或竖直);
‘Q’:既可以走斜线又可以走直线。
需要注意的是,一个棋子要吃另一个的话这两个棋子中间不能隔其他棋子。
问黑棋能否一步吃掉白棋。

思路:直接模拟即可。只要一次判断白棋8个方向最近的那个棋子能否吃掉它。

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int Distance[8][2];//第一维记录方向,第二维记录黑棋种类 //这里要注意C++中Distance是已经存在的函数,所以变量名不能取Distance,晕死 int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<8;i++) //初始化         {            Distance[i][0]=INF<<1;//INF是10^9数量级的但是小于2*10^9,所以这里要乘2             Distance[i][1]=0;        }        int x0,y0,x,y;        char s[3];        scanf("%d%d",&x0,&y0);        for(int i=0;i<n;i++)        {            scanf("%s%d%d",s,&x,&y);            if(abs(x-x0)==abs(y-y0)||x==x0||y==y0)            {                int dis=max(abs(x-x0),abs(y-y0));                int tmp;//记录黑棋在白棋的什么方向                 if(x>x0&&y==y0) tmp=0;                if(x==x0&&y>y0) tmp=1;                if(x<x0&&y==y0) tmp=2;                if(x==x0&&y<y0) tmp=3;                if(x>x0&&y>y0) tmp=4;                if(x<x0&&y>y0) tmp=5;                if(x<x0&&y<y0) tmp=6;                if(x>x0&&y<y0) tmp=7;                if(dis<Distance[tmp][0])                {                    Distance[tmp][0]=dis;                    if(s[0]=='B') Distance[tmp][1]=1;                    if(s[0]=='R') Distance[tmp][1]=2;                    if(s[0]=='Q') Distance[tmp][1]=3;                }            }        }        bool flag=false;        for(int i=0;i<4;i++)            if(Distance[i][1]==2||Distance[i][1]==3) flag=true;        for(int i=4;i<8;i++)            if(Distance[i][1]==1||Distance[i][1]==3) flag=true;        if(flag) printf("YES\n");        else printf("NO\n");    }    return 0;}
原创粉丝点击