Codeforces 734D. Anton and Chess (暴力 + 二分)

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 x 0 and y 0 ( - 109 ≤ x 0, y 0 ≤ 109) — coordinates of the white king.

Then follow n lines, each of them contains a character and two integers x i and y i ( - 109 ≤ x i, y i ≤ 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
2
4 2
R 1 1
B 1 5
Output
YES

Input
2
4 2
R 3 3
B 1 5
Output
NO

Solution
暴力 + 二分

Hint
对pair数组 lower_bound/upper_bound 第三个参数也得为pair,若是寻找数组中存在的元素,则first 和 second必须都相等才能找对位置

Code

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int,int>
#define mpp make_pair
#define fi first
#define se second
const int maxn = 3e6 + 7;
vector<pii>B;
vector<pii>R;
vector<pii>Q;
pii king;
pii ver[maxn];
bool checkver(){
	int cnt = 0;
 	for(auto x : R) if(x.fi == king.fi) ver[++cnt] = {x.se,0};
 	for(auto x : Q) if(x.fi == king.fi) ver[++cnt] = {x.se,0};
 	for(auto x : B) if(x.fi == king.fi) ver[++cnt] = {x.se,1};
 	ver[++cnt] = {king.se,king.fi};
 	sort(ver + 1,ver + 1 + cnt);
 	if(cnt == 1) return false;
 	int kpos = lower_bound(ver + 1,ver + 1 + cnt,mpp(king.se,king.fi)) - ver;
 	if(kpos != 1 && ver[kpos-1].se == 0) return true;
 	if(kpos != cnt && ver[kpos+1].se == 0) return true;
 	return false;
}
pii hor[maxn];
bool checkhor(){
	int cnt = 0;
	for(auto x : R) if(x.se == king.se) hor[++cnt] = {x.fi,0};
	for(auto x : Q) if(x.se == king.se) hor[++cnt] = {x.fi,0};
	for(auto x : B) if(x.se == king.se) hor[++cnt] = {x.fi,1};
	hor[++cnt] = {king.fi,king.se};
	sort(hor + 1,hor + 1 + cnt);
	if(cnt == 1) return false;
	int kpos = lower_bound(hor + 1,hor + 1 + cnt,mpp(king.fi,king.se))- hor;
	if(kpos != 1 && hor[kpos-1].se == 0) return true;
	if(kpos != cnt && hor[kpos+1].se == 0) return true;
	return false;
}
pii dig[maxn];
bool checkdig1(){
	int cnt = 0;
	for(auto x:B) if(x.fi-king.fi == x.se-king.se) dig[++cnt] = {x.fi,0};
	for(auto x:Q) if(x.fi-king.fi == x.se-king.se) dig[++cnt] = {x.fi,0};
	for(auto x:R) if(x.fi-king.fi == x.se-king.se) dig[++cnt] = {x.fi,1};
	dig[++cnt] = {king.fi,king.se};
	sort(dig + 1,dig + 1 + cnt);
	if(cnt == 1) return false;
	int kpos = lower_bound(dig + 1,dig + 1 + cnt,mpp(king.fi,king.se)) - dig;
	if(kpos != 1 && dig[kpos-1].se == 0) return true;
	if(kpos != cnt && dig[kpos+1].se == 0) return true;
	return false; 
}

bool checkdig2(){
	int cnt = 0;
	for(auto x:B) if(x.fi-king.fi == -(x.se-king.se)) dig[++cnt] = {x.fi,0};
	for(auto x:Q) if(x.fi-king.fi == -(x.se-king.se)) dig[++cnt] = {x.fi,0};
	for(auto x:R) if(x.fi-king.fi == -(x.se-king.se)) dig[++cnt] = {x.fi,1};
	dig[++cnt] = {king.fi,king.se};
	sort(dig + 1,dig + 1 + cnt);
	if(cnt == 1) return false;
	int kpos = lower_bound(dig + 1,dig + 1 + cnt,mpp(king.fi,king.se)) - dig;
	if(kpos != 1 && dig[kpos-1].se == 0) return true;
	if(kpos != cnt && dig[kpos+1].se == 0) return true;
	return false; 
}

int main() {
	int n;scanf("%d",&n);
	scanf("%d%d",&king.fi,&king.se);
	// B.assign(n,0);R.assign(n,0);Q.assign(n,0);
	for(int i = 1;i <= n;++i){
		char op[4];scanf("%s",op);
		pii tmp;
		scanf("%d%d",&tmp.fi,&tmp.se);
		if(op[0] == 'B') B.pb(tmp);
		if(op[0] == 'R') R.pb(tmp);
		if(op[0] == 'Q') Q.pb(tmp);
 	}
 	if(checkver() || checkhor() || checkdig1() || checkdig2()) printf("YES\n");
 	else printf("NO\n");
 	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值