Codeforces 734D. 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".
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.
The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise.
题意:就是给你一个很大的棋盘,给你一个白棋的位置还有n个黑棋的位置,问你黑棋能否一步就吃掉白棋
给你如下规则
1.‘B'只能对角线移动,而且不能越过其他黑棋。
2.’R'只能上下左右移动,而且不能越过其他黑棋。
3.‘Q’既能对角线移动又能左右移动,但是不能越过其他黑棋。
其实也就是个模拟题,但也有一些技巧,只要考虑白棋的正上方,正下方,正左方,正右方距离白棋最小的是否为‘R'or’Q'。
斜右上角,斜右下角,斜左下角,斜左上角距离白棋最小的是否为‘B'or’Q'。即可。还有就是要注意一下小细节。
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int M = 5e5 + 10;
struct TnT {
char cp[2];
int x , y;
}s[M];
int main()
{
int t;
scanf("%d" , &t);
int x0 , y0;
scanf("%d%d" , &x0 , &y0);
int flag = 0;
for(int i = 0 ; i < t ; i++) {
scanf("%s %d %d" , s[i].cp , &s[i].x , &s[i].y);
if(s[i].x == x0 && s[i].y == y0)
flag = 1;
//cout << s[i].cp << ' ' << s[i].x << ' ' << s[i].y << endl;
}
for(int i = 0 ; i < 8 ; i++) {
int temp = 0;
int MIN = 2e9 + 10;
if(i == 0) {
for(int j = 0 ; j < t ; j++) {
if(s[j].x == x0 && s[j].y > y0) {
int gg = abs(s[j].y - y0);
if(MIN > gg) {
temp = j;
MIN = gg;
}
}
}
if((MIN != 2e9 + 10) && (s[temp].cp[0] == 'R' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
if(i == 1) {
for(int j = 0 ; j < t ; j++) {
if(s[j].x == x0 && s[j].y < y0) {
int gg = abs(s[j].y - y0);
if(MIN > gg) {
temp = j;
MIN = gg;
}
}
}
if((MIN != 2e9 + 10) && (s[temp].cp[0] == 'R' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
if(i == 2) {
for(int j = 0 ; j < t ; j++) {
if(s[j].y == y0 && s[j].x > x0) {
int gg = abs(s[j].x - x0);
if(MIN > gg) {
temp = j;
MIN = gg;
}
}
}
if((MIN != 2e9 + 10) && (s[temp].cp[0] == 'R' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
if(i == 3) {
for(int j = 0 ; j < t ; j++) {
if(s[j].y == y0 && s[j].x < x0) {
int gg = abs(s[j].x - x0);
if(MIN > gg) {
temp = j;
MIN = gg;
}
}
}
if((MIN != 2e9 + 10) && (s[temp].cp[0] == 'R' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
ll MIN2 = 9e18;
if(i == 4) {
for(int j = 0 ; j < t ; j++) {
if((s[j].y - y0) == -1 * (s[j].x - x0) && s[j].y > y0 && s[j].x < x0) {
int x1 = abs(s[j].x - x0);
int y1 = abs(s[j].y - y0);
ll gg = (ll)x1 * x1 + (ll)y1 * y1;
if(MIN2 > gg) {
temp = j;
MIN2 = gg;
} }
}
if((MIN2 != 9e18) && (s[temp].cp[0] == 'B' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
if(i == 5) {
for(int j = 0 ; j < t ; j++) {
if((s[j].y - y0) == -1 * (s[j].x - x0) && s[j].y < y0 && s[j].x > x0) {
int x1 = abs(s[j].x - x0);
int y1 = abs(s[j].y - y0);
ll gg = (ll)x1 * x1 + (ll)y1 * y1;
if(MIN2 > gg) {
temp = j;
MIN2 = gg;
} }
}
if((MIN2 != 9e18) && (s[temp].cp[0] == 'B' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
if(i == 6) {
for(int j = 0 ; j < t ; j++) {
if((s[j].y - y0) == (s[j].x - x0) && s[j].y < y0 && s[j].x < x0) {
int x1 = abs(s[j].x - x0);
int y1 = abs(s[j].y - y0);
ll gg = (ll)x1 * x1 + (ll)y1 * y1;
if(MIN2 > gg) {
temp = j;
MIN2 = gg;
} }
}
if((MIN2 != 9e18) && (s[temp].cp[0] == 'B' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
if(i == 7) {
for(int j = 0 ; j < t ; j++) {
if((s[j].y - y0) == (s[j].x - x0) && s[j].y > y0 && s[j].x > x0) {
int x1 = abs(s[j].x - x0);
int y1 = abs(s[j].y - y0);
ll gg = (ll)x1 * x1 + (ll)y1 * y1;
if(MIN2 > gg) {
temp = j;
MIN2 = gg;
} }
}
if((MIN2 != 9e18) && (s[temp].cp[0] == 'B' || s[temp].cp[0] == 'Q')) {
flag = 1;
}
if(flag == 1)
break;
else
continue;
}
if(flag == 1)
break;
}
if(flag == 0)
printf("NO\n");
else
printf("YES\n");
return 0;
}
Codeforces 734D. Anton and Chess(模拟)的更多相关文章
- 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 ...
- D. Anton and Chess 模拟题 + 读题
http://codeforces.com/contest/734/problem/D 一开始的时候看不懂题目,以为象是中国象棋那样走,然后看不懂样例. 原来是走对角线的,长知识了. 所以我们就知道, ...
- 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 ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess —— 基础题
题目链接:http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test 4 seconds me ...
- Anton and Chess
Anton and Chess time limit per test 4 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Anton and Chess(模拟+思维)
http://codeforces.com/group/1EzrFFyOc0/contest/734/problem/D 题意:就是给你一个很大的棋盘,给你一个白棋的位置还有n个黑棋的位置,问你黑棋能 ...
- Codeforces 747C:Servers(模拟)
http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...
随机推荐
- ubuntu中设置python默认版本
看/usr/bin中的Python文件,发现该文件是python2.7的链接文件 于是直接删掉这个软链接,然后重新创建python2.6的链接文件: 1 rm /usr/bin/python 2 ln ...
- oracle常用的一些sql命令
//查看系统当前时间 HH24 24小时制 MI是正确的分钟 select to_char(sysdate,'yyyy-mm-dd HH24:MI:SS') from dual //HH非24 ...
- Java基础之二十 并发
20.1 并发得多面性 并发编程令人困惑的一个主要原因:使用并发时需要解决的问题有多个,而实现并发的方法也有多种,并且在这两者之间没有明显的映射关系. 20.1.1 更快的执行 速度问题初听起来很简单 ...
- JavaFX OnMouseClick
在JavaFX开发环境中,遇到一些坑是难免的,而且资料少得可怜! 先说一下我遇到的问题 : 只是一个点击事件而已 : 首先我有这么个界面 : 接下来呢 ? 我需要点击右上角的X,然后显示遮罩,弹出对话 ...
- Visual Studio Debug
在watch窗口输入,$err,hr可以看到上一个错误代码和相关描述信息 Error Lookup可以将错误代码转换成为相应的文本描述 FormatMessage()
- kafka消息的处理机制(五)
这一篇我们不在是探讨kafka的使用,前面几篇基本讲解了工作中的使用方式,基本api的使用还需要更深入的去钻研,多使用才会有提高.今天主要是探讨一下kafka的消息复制以及消息处理机制. 1. bro ...
- HTML/CSS:导航栏水平和垂直
1.垂直导航栏 导航栏 = 链接列表导航栏基本上是一个链接列表,因此使用 <ul> 和 <li> 元素是非常合适的.如需构建垂直导航栏,我们只需要定义 <a> 元素 ...
- 算法与数据结构基础 - 合并查找(Union Find)
Union Find算法基础 Union Find算法用于处理集合的合并和查询问题,其定义了两个用于并查集的操作: Find: 确定元素属于哪一个子集,或判断两个元素是否属于同一子集 Union: 将 ...
- 读取某个目录下的所有图片并显示到pictureBox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Windows Server 2008利用NTFS管理数据
今天我们学习关于NTFS管理数据 以下是学习的内容NTFS分区和FAT32分区的区别,如何将FAT32分区转化成NTFS分区,FAT 32 不支持大于4G ,NTFS权限设置 ,EFS加密 ,文件夹的 ...