D. Anton and Chess
题目连接:
http://codeforces.com/contest/734/problem/D
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.
Sample Input
2
4 2
R 1 1
B 1 5
Sample Output
YES
Hint
题意
一个无限大的平面,给你一个King的位置,再给你其他棋子的位置,问你这个King是不是正在被人将军。
题解:
只用考虑King的八个方向最近的棋子是什么就好了,记录一下就行了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+6;
long long x[maxn],y[maxn],xx,yy;
char a[3][3];
long long dis[3][3];
string s;
int f(int x){
if(x==0)return 1;
if(x>0)return 2;
if(x<0)return 0;
}
int main()
{
int n;scanf("%d",&n);
scanf("%lld%lld",&xx,&yy);
for(int i=0;i<3;i++)for(int j=0;j<3;j++)dis[i][j]=2e15,a[i][j]='W';
for(int i=1;i<=n;i++){
cin>>s>>x[i]>>y[i];
x[i]-=xx,y[i]-=yy;
if(x[i]!=0&&y[i]!=0&&abs(x[i])!=abs(y[i]))continue;
if(dis[f(x[i])][f(y[i])]>max(abs(x[i]),abs(y[i]))){
dis[f(x[i])][f(y[i])]=max(abs(x[i]),abs(y[i]));
a[f(x[i])][f(y[i])]=s[0];
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(a[i][j]=='Q')return puts("YES");
if((i+j)%2==0&&a[i][j]=='B')return puts("YES");
if((i+j)%2==1&&a[i][j]=='R')return puts("YES");
}
}
puts("NO");
}