Codeforces Round #379 (Div. 2) Anton and Chess

来源:互联网 发布:淘宝图片轮播什么意识 编辑:程序博客网 时间:2024/05/18 01:12

D. 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 on8 to8 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 andy0 ( - 109 ≤ x0, y0 ≤ 109) — coordinates of the white king.

Then follow n lines, each of them contains a character and two integersxi andyi ( - 109 ≤ xi, yi ≤ 109) — type of thei-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
24 2R 1 1B 1 5
Output
YES
Input
24 2R 3 3B 1 5
Output
NO

解:找出里白国王最近的8个黑棋的位置,特判这些为哪些棋子即可。


代码:

#include <bits/stdc++.h>using namespace std;char s[10];int main(){    long long n,x,y,nowx,nowy;    cin>>n>>x>>y;    long long north,south,east,west,en,es,wn,ws;    north=south=east=west=en=es=wn=ws=2e10+5;    int northjudge,southjudge,eastjudge,westjudge,enjudge,esjudge,wnjudge,wsjudge;    for(int i=1;i<=n;i++)    {        scanf("%s %I64d %I64d",s,&nowx,&nowy);        if(s[0]=='B')        {            if(nowy<y&&nowx==x)            {                if(y-nowy<west)                {                    westjudge=1;                    west=y-nowy;                }            }            if(nowy>y&&nowx==x)            {                if(-y+nowy<east)                {                    eastjudge=1;                    east=-y+nowy;                }            }            if(nowx>x&&nowy==y)            {                if(nowx-x<south)                {                    south=nowx-x;                    southjudge=1;                }            }            if(nowx<x&&nowy==y)            {                if(-nowx+x<north)                {                    north=-nowx+x;                    northjudge=1;                }            }            if(nowx+nowy==x+y)            {                if(nowx>x)                {                    if(nowx-x+y-nowy<ws)                    {                         ws=nowx-x+y-nowy;                         wsjudge=1;                    }                }                else                {                    if(-nowx+x-y+nowy<en)                    {                         en=-nowx+x-y+nowy;                         enjudge=1;                    }                }            }            if(nowx-x==nowy-y)            {                if(nowx>x)                {                    if(nowx-x-y+nowy<es)                    {                         es=nowx-x-y+nowy;                         esjudge=1;                    }                }                else                {                    if(-nowx+x+y-nowy<wn)                    {                         wn=-nowx+x+y-nowy;                         wnjudge=1;                    }                }            }        }        if(s[0]=='R')        {             if(nowy<y&&nowx==x)            {                if(y-nowy<west)                {                    westjudge=2;                    west=y-nowy;                }            }            if(nowy>y&&nowx==x)            {                if(-y+nowy<east)                {                    eastjudge=2;                    east=-y+nowy;                }            }            if(nowx>x&&nowy==y)            {                if(nowx-x<south)                {                    south=nowx-x;                    southjudge=2;                }            }            if(nowx<x&&nowy==y)            {                if(-nowx+x<north)                {                    north=-nowx+x;                    northjudge=2;                }            }            if(nowx+nowy==x+y)            {                if(nowx>x)                {                    if(nowx-x+y-nowy<ws)                    {                         ws=nowx-x+y-nowy;                         wsjudge=2;                    }                }                else                {                    if(-nowx+x-y+nowy<en)                    {                         en=-nowx+x-y+nowy;                         enjudge=2;                    }                }            }            if(nowx-x==nowy-y)            {                if(nowx>x)                {                    if(nowx-x-y+nowy<es)                    {                         es=nowx-x-y+nowy;                         esjudge=2;                    }                }                else                {                    if(-nowx+x+y-nowy<wn)                    {                         wn=-nowx+x+y-nowy;                         wnjudge=2;                    }                }            }        }        if(s[0]=='Q')        {             if(nowy<y&&nowx==x)            {                if(y-nowy<west)                {                    westjudge=3;                    west=y-nowy;                }            }            if(nowy>y&&nowx==x)            {                if(-y+nowy<east)                {                    eastjudge=3;                    east=-y+nowy;                }            }            if(nowx>x&&nowy==y)            {                if(nowx-x<south)                {                    south=nowx-x;                    southjudge=3;                }            }            if(nowx<x&&nowy==y)            {                if(-nowx+x<north)                {                    north=-nowx+x;                    northjudge=3;                }            }            if(nowx+nowy==x+y)            {                if(nowx>x)                {                    if(nowx-x+y-nowy<ws)                    {                         ws=nowx-x+y-nowy;                         wsjudge=3;                    }                }                else                {                    if(-nowx+x-y+nowy<en)                    {                         en=-nowx+x-y+nowy;                         enjudge=3;                    }                }            }            if(nowx-x==nowy-y)            {                if(nowx>x)                {                    if(nowx-x-y+nowy<es)                    {                         es=nowx-x-y+nowy;                         esjudge=3;                    }                }                else                {                    if(-nowx+x+y-nowy<wn)                    {                         wn=-nowx+x+y-nowy;                         wnjudge=3;                    }                }            }        }    }    int flag=0;    if(eastjudge!=1&&east!=2e10+5)  flag=1;    if(westjudge!=1&&west!=2e10+5)  flag=1;    if(northjudge!=1&&north!=2e10+5)  flag=1;    if(southjudge!=1&&south!=2e10+5)  flag=1;    if(enjudge!=2&&en!=2e10+5)  flag=1;    if(esjudge!=2&&es!=2e10+5)  flag=1;    if(wnjudge!=2&&wn!=2e10+5)  flag=1;    if(wsjudge!=2&&ws!=2e10+5)  flag=1;    if(flag) puts("YES");        else puts("NO");    return 0;}



0 0
原创粉丝点击