【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
2
4 2
R 1 1
B 1 5
output
YES
input
2
4 2
R 3 3
B 1 5
output
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”.
【题目链接】:http://codeforces.com/contest/734/problem/D
【题解】
一开始想错了;还以为是一个棋子一个棋子地摆下去;后来才发现;原来是全部摆完之后才判断;
做法是把那个白皇后的横、竖、两个对角线上的坐标记录一下;(i+j)就在’/’型对角线上,(i-j)在’\’型对角线上.用4个vector记录;
然后按照y坐标排序(除了竖的线按照x坐标之外);
(当然,你要把那个白皇后一开始也加到这4个vector中);
然后用个lower_bound查找那个白皇后在4个vector中的位置;
然后看4个vector中该白皇后相邻的两个棋子会不会攻击到这个棋子;
横的和竖的有皇后和车能攻击到
对角线则有皇后和象能攻击到;
具体的看代码吧;
【完整代码】
#include <bits/stdc++.h>
#define LL long long
using namespace std;
struct abc
{
LL y;
char key;
};
int n;
LL a0,b0;
vector <abc> heng,shu;
vector <abc> xpy,xsy;
abc temp;
bool cmp(abc a,abc b)
{
return a.y < b.y;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> n;
cin >> a0 >>b0;
temp.y = b0;
heng.push_back(temp);
temp.y = a0;
shu.push_back(temp);
temp.y = b0;temp.key = '*';
xpy.push_back(temp);
xsy.push_back(temp);
for (int i = 1;i <= n;i++)
{
LL x,y;
char key;
cin >> key >> x >> y;
temp.y = y;temp.key = key;
if (x == a0)
heng.push_back(temp);
temp.y = x;
if (y == b0)
shu.push_back(temp);
temp.y = y;
if (x+y==a0+b0)
xpy.push_back(temp);
if (x-y==a0-b0)
xsy.push_back(temp);
}
sort(heng.begin(),heng.end(),cmp);
sort(shu.begin(),shu.end(),cmp);
sort(xpy.begin(),xpy.end(),cmp);
sort(xsy.begin(),xsy.end(),cmp);
int pos;
//横线
temp.y = b0;
pos = lower_bound(heng.begin(),heng.end(),temp,cmp)-heng.begin();
int len = heng.size();
if (pos-1>=0)
{
char key = heng[pos-1].key;
if (key == 'Q' || key == 'R')
{
puts("YES");
return 0;
}
}
if (pos+1 <= len-1)
{
char key = heng[pos+1].key;
if (key == 'Q' || key == 'R')
{
puts("YES");
return 0;
}
}
len = shu.size();
//竖线
temp.y = a0;
pos = lower_bound(shu.begin(),shu.end(),temp,cmp)-shu.begin();
if (pos-1>=0)
{
char key = shu[pos-1].key;
if (key == 'Q' || key == 'R')
{
puts("YES");
return 0;
}
}
if (pos+1 <= len-1)
{
char key = shu[pos+1].key;
if (key == 'Q' || key == 'R')
{
puts("YES");
return 0;
}
}
len = xpy.size();
// '/'型对角线
temp.y = b0;
pos = lower_bound(xpy.begin(),xpy.end(),temp,cmp)-xpy.begin();
if (pos-1>=0)
{
char key = xpy[pos-1].key;
if (key == 'Q' || key == 'B')
{
puts("YES");
return 0;
}
}
if (pos+1 <= len-1)
{
char key = xpy[pos+1].key;
if (key == 'Q' || key == 'B')
{
puts("YES");
return 0;
}
}
len = xsy.size();
// '\'型对角线
pos = lower_bound(xsy.begin(),xsy.end(),temp,cmp)-xsy.begin();
if (pos-1>=0)
{
char key = xsy[pos-1].key;
if (key == 'Q' || key == 'B')
{
puts("YES");
return 0;
}
}
if (pos+1 <= len-1)
{
char key = xsy[pos+1].key;
if (key == 'Q' || key == 'B')
{
puts("YES");
return 0;
}
}
puts("NO");
return 0;
}
【29.89%】【codeforces 734D】Anton and Chess的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【74.89%】【codeforces 551A】GukiZ and Contest
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 29B】Traffic Lights
[题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
随机推荐
- 洛谷 P2969 [USACO09DEC]音符Music Notes
P2969 [USACO09DEC]音符Music Notes 题目描述 FJ is going to teach his cows how to play a song. The song cons ...
- NHibernate3剖析:Mapping篇之集合映射基础(2):Bag映射
系列引入 NHibernate3.0剖析系列分别从Configuration篇.Mapping篇.Query篇.Session策略篇.应用篇等方面全面揭示NHibernate3.0新特性和应用及其各种 ...
- 2018VMware虚拟机安装Mac OS 10.12.1
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.下载安装中所需的镜像文件以及补丁工具 Mac OS 10.12.1 Sierra (16B2555) 懒人版(下载地址):ht ...
- 【例题 6-11 UVA-297】Quadtrees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 发现根本不用存节点信息. 遇到了叶子节点且为黑色,就直接覆盖矩阵就好(因为是并集); [代码] #include <bits/ ...
- linux cmd cp -a
cp -a 在保留原文件属性的前提下复制文件 cp -r dirname destdir 复制目录后其文件属性会发生变化 想要使得复制之后的目录和原目录完全一样,可以使用cp -a dirn ...
- 【Codeforces Round #440 (Div. 2) B】Maximum of Maximums of Minimums
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] k=1的时候就是最小值, k=2的时候,暴力枚举分割点. k=3的时候,最大值肯定能被"独立出来",则直接输出最 ...
- AE地图查询
原文 AE地图查询 地图查询主要有两种查询:空间查询和属性查询 所用到知识点: 1 Cursor(游标)对象 本质上是一个指向数据的指针,本身不包含数据内容,提供一个连接到ROW对象或者要素对象(F ...
- OpenGL_ES-纹理
OpenGL_ES2.0 -纹理 一:纹理基础: 1: 纹素的概念: 一个二维纹理在OpenGLES2.0中是非经常见的,二维纹理就是一个二维数组,每一个数据元素称为纹素,详细格式例如以下: GL_R ...
- 特征点提取之Harris角点提取法
1. 特征点提取的意义 2.角点 3. Harris角点检測的基本原理 4.Harris角点检測算法的步骤 5.Harris角点提取算法设计 <span style="font-siz ...
- COCOS2D-X 3.0在MAC下创建新IOS项目:
首先进入:CocoStudio\Source\3.0\cocos2d-x\tools\cocos2d-console\bin 运行 ./cocos new -p com.aaaa -l cpp MyG ...