题目链接:http://codeforces.com/contest/734/problem/D

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
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"

题解:

1.在King的八个方向上,分别记录离其最近的棋子。

2.如果在横、竖线上,存在Rook或者Queen,则King in check; 如果在对角线上,存在Bishop或者Queen, 则King in check。

学习之处:

对角线的表示:

1.左上右下: x-y (注意:如果需要用非负数表示,则x-y+n)

2.右上左下:x+y

代码如下;

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 1e5+10; int n, xo, yo;
int a[10][5];
map<string, int>m; void f(int x, int y, int id) //分别在八个方向上取最近的棋
{
if(x==xo)
{
if(y<yo && (a[1][1]==INF || y>a[1][2]) )
a[1][1] = x, a[1][2] = y, a[1][3] = id;
if(y>yo && ( a[2][1]==INF || y<a[2][2]) )
a[2][1] = x, a[2][2] = y, a[2][3] = id;
}
else if(y==yo)
{
if(x<xo && ( a[3][1]==INF || x>a[3][1] ) )
a[3][1] = x, a[3][2] = y, a[3][3] = id;
if(x>xo && ( a[4][1]==INF || x<a[4][1]) )
a[4][1] = x, a[4][2] = y, a[4][3] = id;
}
else if(x-y==xo-yo)
{
if(x<xo && ( a[5][1]==INF || x>a[5][1]) )
a[5][1] = x, a[5][2] = y, a[5][3] = id;
if(x>xo && ( a[6][1]==INF || x<a[6][1]) )
a[6][1] = x, a[6][2] = y, a[6][3] = id;
}
else if(x+y==xo+yo)
{
if(x>xo && ( a[7][1]==INF || x<a[7][1]) )
a[7][1] = x, a[7][2] = y, a[7][3] = id;
if(x<xo && ( a[8][1]==INF || x>a[8][1]) )
a[8][1] = x, a[8][2] = y, a[8][3] = id;
}
} int main()
{
scanf("%d%d%d",&n,&xo, &yo); m["B"] = 1; m["R"] = 2; m["Q"] = 3;
for(int i = 0; i<10; i++)
a[i][1] = INF; string s; int x, y;
for(int i = 1; i<=n; i++)
{
cin>>s>>x>>y;
f(x, y, m[s]);
} int B = 0;
for(int i = 1; i<=4; i++) //横、竖
if(a[i][1]!=INF && ( a[i][3]==2 || a[i][3]==3))
B = 1;
for(int i = 5; i<=8; i++) //对角线
if(a[i][1]!=INF && ( a[i][3]==1 || a[i][3]==3))
B = 1;
printf("%s\n", B? "YES" : "NO");
}

Codeforces Round #379 (Div. 2) D. Anton and Chess —— 基础题的更多相关文章

  1. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  2. Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟

    题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...

  3. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  4. Codeforces Round #379 (Div. 2) A. Anton and Danik 水题

    A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...

  5. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  6. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  7. Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路

    题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...

  8. Codeforces Round #379 (Div. 2) C. Anton and Making Potions —— 二分

    题目链接:http://codeforces.com/contest/734/problem/C C. Anton and Making Potions time limit per test 4 s ...

  9. Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径

    E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. gradle_____最后到齐的构建工具

    从今年开始,开始换用gradle 了,个人感觉还好,配置不像maven,一堆xml 文件,一个jar 一行字符,内置的task 和很多.自定义task 也挺简单,比ant简单一些. 简单配置文件示例: ...

  2. PyTorch学习笔记之nn的简单实例

    method 1 import torch from torch.autograd import Variable N, D_in, H, D_out = 64, 1000, 100, 10 x = ...

  3. javascript 函数初探 (五)--- 几种类型的函数

    即时函数: 目前我们已经讨论了匿名函数在回调时的应用.接下来,我们来看看匿名函数的另一种应用实例 --- javascript即时函数: 比如: ( function(){ alert('her'); ...

  4. hdu 5389 Zero Escape (dp)

    题目:http://acm.hdu.edu.cn/showproblem.php? pid=5389 题意:定义数根:①把每一位上的数字加起来得到一个新的数,②反复①直到得到的数仅仅有1位.给定n,A ...

  5. (一)MVVMLight安装

    http://www.cnblogs.com/manupstairs/p/4890300.html 1.首先新建一个wpf项目 2. 安装完成即可在我们的项目中看到如下引用: 如果点击安装的时候出现: ...

  6. Web框架Django(二)

    一.Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层 ...

  7. Linux 中如何查询和卸载已安装的软件

    Linux 中如何查询和卸载已安装的软件 Linux软件的安装和卸载一直是困扰许多新用户的难题.在Windows中,我们可以使用软件自带的安装卸载程序或在控制面板中的“添加/删除程序” 来实现.与其相 ...

  8. caffe学习--使用caffe中的imagenet对自己的图片进行分类训练(超级详细版) -----linux

    http://blog.csdn.net/u011244794/article/details/51565786 标签: caffeimagenet 2016-06-02 12:57 9385人阅读  ...

  9. React - S1

    资料: 1. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript 进度: 教程 - 高级内容remaining; 参考remaining j ...

  10. Struts2 (三) (转载)

    前面一直在说Action可以是一个普通的Java类,与Servlet API完全分离,但是为了实现业务逻辑,Action需要使用HttpServletRequest内容.Struts 2设计的精巧之处 ...