Anton and Chess(皇后问题)

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 ( - \(10^9\)≤ x0, y0 ≤ \(10^9\)) — coordinates of the white king.

Then follow n lines, each of them contains a character and two integers xi and yi( - \(10^9\)≤ x0, y0 ≤ \(10^9\)) — 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.

题意:给定一个无限残局,已知:黑king坐标,n个Bishop、Rook、Queen坐标,Bishop符号 ' B '(只能走同行或者同列) , Rook符号 ' R '(只能走对角线) ,Queen 符号 ' Q ' (能走 ‘ B ’ 和 ' Q ' 所走的)问这个残局能否将黑king吃掉

思路:已知黑king坐标,可以将每次读入的BRQ坐标依次处理,找出黑king八个方向距离最近的坐标及符号(因为题目限制不能跳棋)。八个方向坐标的储存可以利用pair的排序规则,先比对key再比对value,八个方向可以将上下左右和对角线分成两个部分最后作判断。

皇后问题的坐标转换:

eg:某个点的坐标(x0,y0)另一点坐标(x,y)

x==x0 \(\Longleftrightarrow\) 同行

y==y0 \(\Longleftrightarrow\)同列

x0+y0==x+y \(\Longleftrightarrow\)主对角线

|x0-y0|==|x-y| \(\Longleftrightarrow\)副对角线

坐标

部分代码:

//这里的x和y是输入的坐标减去黑King坐标后的坐标(与黑king坐标的差值)
//同行或者同列
if(abs(x)==0||abs(y)==0){
    if(!x){
        if(y>0)pos=4;//右
        else pos=3;//左
	}
    if(!y){
        if(x>0)pos=2;//下
        else pos=1;//上
	}
}
//对角线
if(abs(x)==abs(y)){
    if(x>0){
        if(y>0)pos=8;
        else pos=7;
	}
    else {
        if(y>0)pos=6;
        else pos=5;
	}
}
——————————————————————————————————————————————
//记得每次操作都要将结果更新
int d = max(abs(x), abs(y));
q[pos] = min(q[pos], PII(d, check(op[0])));   
//最后将pair里面八个方位进行判断
for (int i = 1; i <= 8; i++){
	if (i <= 4){
		if (q[i].second == 2 || q[i].second == 3){
			cout << "YES" << endl;
             return 0;
		}
	}
	else if (q[i].second == 1 || q[i].second == 3){
		cout << "YES" << endl;
		return 0;
	}
}
posted @ 2022-01-29 22:14  snaliuu  阅读(47)  评论(0)    收藏  举报