Codeforces 734D Anton and Chess【思维】

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 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个黑色的棋子,对应白色的棋子的位子也是已知的。

已知黑色棋子一共有三种:

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");
    }
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值