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".
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.
The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise.
2 4 2 R 1 1 B 1 5
YES
2 4 2 R 3 3 B 1 5
NO
Picture for the first sample:
Picture for the second sample:
三种棋子: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;
}