CodeForces - 734D Anton and Chess (思维题)

点击打开链接

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".
题意:棋盘上有一个白皇后,还有另外三种棋子,问此时是否已经将军。

三种棋子:B只能对角线攻击;R只能横竖攻击;Q可以对角线以及横竖攻击。但是每个棋子都不能越过其他棋子进行攻击。

题解:就是一个很水的思维题了,把八个方向(上下左右以及四个对角线)的所有棋子都用vector存储起来,然后根据距离白皇后的距离进行排序,看八个方向的第一个棋子能否将白皇后将军即可。


#include<stdio.h>
#include<iostream>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
using namespace std;
const int inf = 0x3f3f3f3f ;
const int mx = 5e5 +5 ;
struct node{
    char ch;
    int x , y ;
    node(){}
    node(char a , int b , int c) {
        ch = a ;
        x = b ;
        y = c ;
    }
};
vector<node> vec[10] ;
void init() {
    for(int i = 0 ; i < 10 ; i ++)
        vec[i].clear() ;
}
bool cmp1(node x , node y) {
    return x.x < y.x ;
}
bool cmp2(node x , node y) {
    return x.y < y.y ;
}
bool cmp3(node x , node y) {
    return x.x > y.x;
}
bool cmp4(node x , node y) {
    return x.y > y.y ;
}
int main()
{
    //freopen ("in.txt", "r", stdin);
    int n ;
    int xi , yi ;
    while(~scanf("%d" , &n)) {
        init();
        scanf("%d%d" , &xi , &yi) ;
        for(int i = 0 ; i < n ; i ++){
            char op[2] ;
            int x , y ;
            scanf("%s %d %d" , op , &x , &y);
            if(x == xi && y - yi > 0)//分八个方向存储
                vec[0].push_back(node(op[0] , x , y));
            else if(x == xi && y - yi < 0)
                vec[1].push_back(node(op[0] , x , y));
            else if(x - xi > 0 && y == yi)
                vec[2].push_back(node(op[0] , x , y));
            else if(x - xi < 0 && y == yi)
                vec[3].push_back(node(op[0] , x , y));
            else if(x - xi > 0 && y - yi > 0 && abs(x - xi) == abs(y - yi))
                vec[4].push_back(node(op[0] , x , y));
            else if(x - xi > 0 && y - yi < 0 && abs(x - xi) == abs(y - yi))
                vec[5].push_back(node(op[0] , x , y));
            else if(x - xi < 0 && y - yi > 0 && abs(x - xi) == abs(y - yi))
                vec[6].push_back(node(op[0] , x , y));
            else if(x - xi < 0 && y - yi < 0 && abs(x - xi) == abs(y - yi))
                vec[7].push_back(node(op[0] , x , y));
        }
        bool flag = false ;
        node p;
        if(!vec[0].empty()){//分八个方向找距离白皇后最近的点
            sort(vec[0].begin() , vec[0].end() , cmp2) ;
            p = vec[0].front();
            //cout<<p.x<<" "<<p.y<<endl;
            if(p.ch != 'B')
                flag = true;
        }

        if(!vec[1].empty()){
            sort(vec[1].begin() , vec[1].end() , cmp4) ;
            p = vec[1].front();
            if(p.ch != 'B')
                flag = true;
        }

        if(!vec[2].empty()) {
            sort(vec[2].begin() , vec[2].end() , cmp1) ;
            p = vec[2].front();
            if(p.ch != 'B')
                flag = true;
        }

        if(!vec[3].empty()) {
            sort(vec[3].begin() , vec[3].end() , cmp3) ;
            p = vec[3].front();
            if(p.ch != 'B')
                flag = true;
        }

        if(!vec[4].empty()) {
            sort(vec[4].begin() , vec[4].end() , cmp1) ;
            p = vec[4].front();
            if(p.ch != 'R')
                flag = true;
        }

        if(!vec[5].empty()) {
            sort(vec[5].begin() , vec[5].end() , cmp1) ;
            p = vec[5].front();
            if(p.ch != 'R')
                flag = true;
        }

        if(!vec[6].empty()) {
            sort(vec[6].begin() , vec[6].end() , cmp3) ;
            p = vec[6].front();
            if(p.ch != 'R')
                flag = true;
        }

        if(!vec[7].empty()) {
            sort(vec[7].begin() , vec[7].end() , cmp3) ;
            p = vec[7].front();
            if(p.ch != 'R')
                flag = true;
        }
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值