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的时间.每次选择机器从小到大开 ...
随机推荐
- go杂货铺
json序列化 内存中变成可存储或传输的过程称之为序列化(dict,split,struct转string) package main import ( "encoding/json&quo ...
- 定制开发kubernetes流程
kubernetes集群三步安装 概述 本文介绍如何对kubernetes进行二次开发,仓库如何管理,git分支如何管理,怎样利用CI去编译与发布以及如何给社区贡献代码等,结合实际例子,望对大家有所帮 ...
- 解决Activiti5.22流程图部署在Windows上正常,但在linux上部署后出现中文变方块的问题
总结/朱季谦 楼主最近在做公司的工作流平台,发现一个很无语的事情,Activiti5.22的流程图在Windows环境上部署,是可以正常查看的,但发布到公司的Linux服务器上后,在上面进行流程图在线 ...
- Go中的函数和闭包
函数参数和返回值的写法 如果有多个参数是同一个类型,可以简略写: func testReturnFunc(v1,v2 int)(int,int) { x1 := 2 * v1 x2 := 3 * v2 ...
- linux下安装开发环境
jdk 下载jdk安装包,解压到/usr/java/jdk 配置环境变量: #vi /etc/profile 在该profile文件中最下面添加: JAVA_HOME=/usr/java/jdk1.7 ...
- 转载 | 一种让超大banner图片不拉伸、全屏宽、居中显示的方法
现在很多网站的Banner图片都是全屏宽度的,这样的网站看起来显得很大气.这种Banner一般都是做一张很大的图片,然后在不同分辨率下都是显示图片的中间部分.实现方法如下: <html> ...
- 11个rsync使用实例
rsync表示 remote sync,其用于在本地或与远程主机间进行文件或目录备份.相比较scp等工具,rsync有以下优点: 速度:除首次全拷贝外,其他时候实现增量拷贝,加快传输速度 安全:传输数 ...
- 如何使用dmidecode命令查看硬件信息
引言 当我们需要获取机器硬件信息时,可使用linux系统自带的dmidecode工具进行查询. dmidecode命令通过读取系统DMI表,显示服务器硬件和BIOS信息.除了可使用dmidecode查 ...
- [转载]ActiveMQ实现负载均衡+高可用部署方案
转载于 http://www.open-open.com/lib/view/open1400126457817.html 一.架构和技术介绍 1.简介 ActiveMQ 是Apache出品,最流行的, ...
- MySQL-EXPLAIN执行计划字段解释
做 MySQL 查询优化遇到明明建了索引查询仍然很慢,看这个 SQL 的执行计划,看它到底有没有用到索引,执行的具体情况.我们可以用 EXPLAIN 命令查看 SQL 的执行计划,SQL 优化的重要性 ...