CodeForces 734D:Anton and Chess(模拟?)

Anton and Chess

Time limit:4000 ms Memory limit:262144 kB


Problem Description

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


题意:

国际象棋,问白色的国王是否被将军

解题思路:

只要保留八个方向离国王位置最近的就可以了,然后再根据不同棋子的规则看看他们能否将军 感觉自己的代码蠢哭


Code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define mem(a,b) memset(a,0,sizeof(a))
using namespace std;

int x0,y0;
struct Node
{
    int x,y;
    char p;
} pos[10];

bool judge1(int x,int y)//正上
{
    if(y==y0&&x<x0)
        return true;
    return false;
}
bool judge2(int x,int y)//右上
{
    if(x+y==x0+y0&&y>y0)
        return true;
    return false;
}
bool judge3(int x,int y)//正右
{
    if(x==x0&&y>y0)
        return true;
    return false;
}
bool judge4(int x,int y)//右下
{
    if(x-y==x0-y0&&y>y0)
        return true;
    return false;
}
bool judge5(int x,int y)//正下
{
    if(y==y0&&x>x0)
        return true;
    return false;
}
bool judge6(int x,int y)//左下
{
    if(x+y==x0+y0&&y<y0)
        return true;
    return false;
}
bool judge7(int x,int y)//正左
{
    if(x==x0&&y<y0)
        return true;
    return false;
}
bool judge8(int x,int y)//左上
{
    if(x-y==x0-y0&&y<y0)
        return true;
    return false;
}

int main()
{
    int n;
    for(int i=1; i<=8; i++)
        pos[i].p='K';
    scanf("%d",&n);
    scanf("%d%d",&x0,&y0);
    for(int i=0; i<n; i++)
    {
        int x,y;
        char p;
        getchar();
        scanf("%c%d%d",&p,&x,&y);
        if(judge1(x,y))//正上
        {
            if(pos[1].p=='K'||(x>pos[1].x))
                pos[1]=Node{x,y,p};
        }
        else if(judge2(x,y))//右上
        {
            if(pos[2].p=='K'||(y<pos[2].y))
                pos[2]=Node{x,y,p};
        }
        else if(judge3(x,y))//正右
        {
            if(pos[3].p=='K'||(y<pos[3].y))
                pos[3]=Node{x,y,p};
        }
        else if(judge4(x,y))//右下
        {
            if(pos[4].p=='K'||(y<pos[4].y))
                pos[4]=Node{x,y,p};
        }
        else if(judge5(x,y))//正下
        {
            if(pos[5].p=='K'||(x<pos[5].x))
                pos[5]=Node{x,y,p};
        }
        else if(judge6(x,y))//左下
        {
            if(pos[6].p=='K'||(y>pos[6].y))
                pos[6]=Node{x,y,p};
        }
        else if(judge7(x,y))//正左
        {
            if(pos[7].p=='K'||(y>pos[7].y))
                pos[7]=Node{x,y,p};
        }
        else if(judge8(x,y))//左上
        {
            if(pos[8].p=='K'||(y>pos[8].y))
                pos[8]=Node{x,y,p};
        }
    }
    int flag=0;
    for(int i=1;i<=8;i++)
        {
            if(pos[i].p=='K')
                continue;

            if(pos[i].p=='Q')
            {
                flag=1;
                break;
            }
            if(pos[i].p=='R'&&(i%2==1))
            {
                flag=1;
                break;
            }
            if(pos[i].p=='B'&&(i%2==0))
            {
                flag=1;
                break;
            }
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值