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
2
4 2
R 1 1
B 1 5
YES
2
4 2
R 3 3
B 1 5
NO
Note
Picture for the first sample:
Picture for the second sample:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #define lld I64d 5 6 using namespace std; 7 8 char c[10]; 9 long long d[10]; 10 11 int main() 12 { 13 long long n,x,y,a,b; 14 bool flag=false; 15 char z; 16 for(int i=1;i<=8;i++) 17 d[i]=1LL<<32; 18 scanf("%I64d",&n); 19 scanf("%I64d %I64d",&x,&y); 20 for(int i=0;i<n;i++) 21 { 22 getchar(); 23 scanf("%c%lld%lld",&z,&a,&b); 24 if(a==x&&b>y&&(b-y)<d[1]) 25 { 26 d[1]=b-y; 27 c[1]=z; 28 } 29 else 30 { 31 if((a-x)==(b-y)&&a>x&&(a-x)<d[2]) 32 { 33 d[2]=a-x; 34 c[2]=z; 35 } 36 else 37 { 38 if(b==y&&a>x&&(a-x)<d[3]) 39 { 40 d[3]=a-x; 41 c[3]=z; 42 } 43 else 44 { 45 if((a-x)==(y-b)&&a>x&&(a-x)<d[4]) 46 { 47 d[4]=a-x; 48 c[4]=z; 49 } 50 else 51 { 52 if(a==x&&y>b&&(y-b)<d[5]) 53 { 54 d[5]=y-b; 55 c[5]=z; 56 } 57 else 58 { 59 if((x-a)==(y-b)&&x>a&&(x-a)<d[6]) 60 { 61 d[6]=x-a; 62 c[6]=z; 63 } 64 else 65 { 66 if(y==b&&x>a&&(x-a)<d[7]) 67 { 68 d[7]=x-a; 69 c[7]=z; 70 } 71 else 72 { 73 if((x-a)==(b-y)&&x>a&&(x-a)<d[8]) 74 { 75 d[8]=x-a; 76 c[8]=z; 77 //cout<<d[8]<<c[8]<<endl; 78 } 79 } 80 } 81 } 82 } 83 } 84 } 85 } 86 } 87 // cout<<d[8]<<c[8]<<endl; 88 for(int i=1;i<8;i=i+2) 89 { 90 if(flag) 91 break; 92 if(c[i]=='Q'||c[i]=='R') 93 flag=true; 94 } 95 for(int i=2;i<=8;i=i+2) 96 { 97 if(flag) 98 break; 99 if(c[i]=='Q'||c[i]=='B') 100 { 101 flag=true; 102 //cout<<i<<endl; 103 } 104 } 105 if(flag) 106 printf("YES"); 107 else 108 printf("NO"); 109 110 111 return 0; 112 }