Codeforces Round #379 (Div. 2) D. 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个黑棋的位置,问你黑棋能否一步就吃掉白棋

给你如下规则

1.‘B‘只能对角线移动,而且不能越过其他黑棋。

2.’R‘只能上下左右移动,而且不能越过其他黑棋。

3.‘Q’既能对角线移动又能左右移动,但是不能越过其他黑棋。

 

其实也就是个模拟题,但也有一些技巧,只要考虑白棋的正上方,正下方,正左方,正右方距离白棋最小的是否为‘R‘or’Q‘。

斜右上角,斜右下角,斜左下角,斜左上角距离白棋最小的是否为‘B‘or’Q‘。即可。还有就是要注意一下小细节。


具体思路:

在读取每一个黑棋的时候判断它是否与白棋成对角线关系或者与白棋位于同一x轴或y轴。然后计算一下与白棋的距离,这里的距离用max(abs(x - x0), abs(y - y0))即可,算是一点小技巧吧。算了,具体思路还是写在代码里吧,反正很简单,一下子就可以看懂!

#include<bits/stdc++.h>
using namespace std;
int Next[9][2] = {0, 0, 0, 1, 1, 0, -1, 0, 0, -1, 1, 1, 1, -1, -1, -1, -1, 1};
int Distance[9][2];
int main()
{
    int n, x0, y0, x, y;
    char kind[3];//黑棋的种类
    while(cin >> n)
    {
        scanf("%d%d", &x0, &y0);
        for(int i = 1; i <= 8; i++)
            Distance[i][0] = 2*0x3f3f3f3f;//注意要2倍
        for(int i = 0; i < n; i++)
        {
            int dis, dirx, diry, temp;
            scanf("%s%d%d", kind, &x, &y);
            if(abs(x - x0) == abs(y - y0) || abs(x - x0) == 0 || abs(y - y0) == 0)//如果成对角关系或者同一x轴或y轴
            {
                dis = max(abs(x - x0), abs(y - y0));//取max是为了成对角关系的情况
                if(x > x0)  dirx = 1;//记录一下在白棋的哪个方向,算是一点技巧吧
                if(x == x0) dirx = 0;
                if(x < x0)  dirx = -1;
                if(y > y0)  diry = 1;
                if(y == y0) diry = 0;
                if(y < y0)  diry = -1;
                for(int j = 1; j <= 8; j++)//查看到底是在白棋的哪个方向
                    if(Next[j][0] == dirx && Next[j][1] == diry)
                        temp = j;//给出方向编号
                if(dis < Distance[temp][0])//如果距离更近就更新,因为距离近的就把远的给挡住了,不能跳跃.
                {
                    Distance[temp][0] = dis;
                    if(kind[0] == 'B')    Distance[temp][1] = 1;//给黑棋种类编上号
                    if(kind[0] == 'R')    Distance[temp][1] = 2;
                    if(kind[0] == 'Q')    Distance[temp][1] = 3;
                }
            }
        }
        bool ok = false;
        for(int i = 1; i <= 4; i++)//这边前4个方向对应右/下/左/上
            if(Distance[i][1] == 2 || Distance[i][1] == 3)
                ok = true;
        for(int i = 5; i <= 8; i++)//这边前4个方向对应右下/左下/左上/右上
            if(Distance[i][1] == 1 || Distance[i][1] == 3)
                ok = true;
        if(ok)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值