Algorithm


D. Anton and Chess
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
Copy
2
4 2
R 1 1
B 1 5
output
Copy
YES
input
Copy
2
4 2
R 3 3
B 1 5
output
Copy
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".

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <cmath>

struct chessman {
	char k;
	int x, y;
};

using namespace std;

int main() {
    int n;
    cin >> n;
    
    int kx, ky;
    cin >> kx >> ky;
    
    chessman best_top;
    best_top.k = ' ';
    chessman best_right;
    best_right.k = ' ';
    chessman best_bottom;
    best_bottom.k = ' ';
    chessman best_left;
    best_left.k = ' ';
    
    chessman best_top_right;
    best_top_right.k = ' ';
    chessman best_bottom_right;
    best_bottom_right.k = ' ';
    chessman best_bottom_left;
    best_bottom_left.k = ' ';
    chessman best_top_left;
    best_top_left.k = ' ';
    
    while(n--) {
    	char k;
    	int x, y;
    	cin >> k >> x >> y;

		if(abs(x) + abs(y) == abs(kx) + abs(ky) || abs(x) - abs(y) == abs(kx) - abs(ky)) {
			if(x > kx && y > ky) {
				if(best_top_right.k == ' ' || (x < best_top_right.x && y < best_top_right.y)) {
					best_top_right.k = k;
					best_top_right.x = x;
					best_top_right.y = y;
				}
			} else if(x > kx && y < ky) {
				if(best_bottom_right.k == ' ' || (x < best_bottom_right.x && y < best_bottom_right.y)) {
					best_bottom_right.k = k;
					best_bottom_right.x = x;
					best_bottom_right.y = y;
				}
			} else if(x < kx && y < ky) {
				if(best_bottom_left.k == ' ' || (x > best_bottom_left.x && y > best_bottom_left.y)) {
					best_bottom_left.k = k;
					best_bottom_left.x = x;
					best_bottom_left.y = y;
				}
			} else if(x < kx && y > ky) {
				if(best_top_left.k == ' ' || (x > best_top_left.x && y < best_top_left.y)) {
					best_top_left.k = k;
					best_top_left.x = x;
					best_top_left.y = y;
				}
			}
		} else if(x == kx) {
			if(y > ky) {
				if(best_top.k == ' ' || y < best_top.y) {
					best_top.k = k;
					best_top.x = x;
					best_top.y = y;
				}
			} else if(y < ky) {
				if(best_bottom.k == ' ' || y > best_bottom.y) {
					best_bottom.k = k;
					best_bottom.x = x;
					best_bottom.y = y;
				}
			}
		} else if(y == ky) {
			if(x > kx) {
				if(best_right.k == ' ' || x < best_right.x) {
					best_right.k = k;
					best_right.x = x;
					best_right.y = y;
				}
			} else if(x < kx) {
				if(best_left.k == ' ' || x > best_left.x) {
					best_left.k = k;
					best_left.x = x;
					best_left.y = y;
				}
			}
		}
    }
    
    if(best_top.k == 'Q' || best_top.k == 'R' ||
       best_right.k == 'Q' || best_right.k == 'R' ||
       best_bottom.k == 'Q' || best_bottom.k == 'R' ||
       best_left.k == 'Q' || best_left.k == 'R' ||
       best_top_right.k == 'Q' || best_top_right.k == 'B' ||
       best_bottom_right.k == 'Q' || best_bottom_right.k == 'B' ||
       best_bottom_left.k == 'Q' || best_bottom_left.k == 'B' ||
       best_top_left.k == 'Q' || best_top_left.k == 'B') {
       	cout << "YES" << endl;
    } else {
       	cout << "NO" << endl;
    }
    
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
4 2
R 1 1
B 1 5

Output

x
+
cmd
YES
Advertisements

Demonstration


Codeforces Solution-Anton and Chess-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+