Codeforces Round #379 (Div. 2) D. 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".
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:
![](http://codeforces.com/predownloaded/94/08/9408be50f47e693db5ec6e7290235a3bc878e147.png)
Picture for the second sample:
![](http://codeforces.com/predownloaded/2b/62/2b625b4a73e1b0f091760b1a5d2c6e549be6a8d7.png)
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=5e5+10,M=1e6+10,inf=1e9+10; const ll INF=1e18+10,mod=2147493647; struct is { char ch; ll x,y; }a[N],m[10]; ll xx,yy; char ch[10]; int main() { int n; scanf("%d",&n); scanf("%lld%lld",&xx,&yy); for(int i=1;i<=n;i++) { int x,y; scanf("%s%lld%lld",ch,&x,&y); a[i].ch=ch[0]; a[i].x=x; a[i].y=y; } for(int i=0;i<=8;i++) m[i].x=inf,m[i].y=inf; for(int i=1;i<=n;i++) { if(a[i].x+a[i].y==xx+yy) { if(a[i].x>xx) { if(m[1].x==inf||a[i].x<m[1].x) { m[1]=a[i]; } } else if(a[i].x<xx) { if(m[3].x==inf||a[i].x>m[3].x) { m[3]=a[i]; } } } if(a[i].x-a[i].y==xx-yy) { if(a[i].x<xx) { if(m[2].x==inf||a[i].x>m[2].x) { m[2]=a[i]; } } else if(a[i].x>xx) { if(m[4].x==inf||a[i].x<m[4].x) { m[4]=a[i]; } } } if(a[i].x==xx) { if(a[i].y>yy) { if(m[8].x==inf||a[i].y<m[8].y) { m[8]=a[i]; } } else if(a[i].y<yy) { if(m[6].x==inf||a[i].y>m[6].y) m[6]=a[i]; } } if(a[i].y==yy) { if(a[i].x>xx) { if(m[5].x==inf||a[i].x<m[5].x) { m[5]=a[i]; } } else if(a[i].x<xx) { if(m[7].x==inf||a[i].x>m[7].x) { m[7]=a[i]; } } } } for(int i=1;i<=4;i++) { if(m[i].ch=='Q'||m[i].ch=='B') return puts("YES"); } for(int i=5;i<=8;i++) if(m[i].ch=='Q'||m[i].ch=='R') return puts("YES"); puts("NO"); return 0; }