【Codeforces 734 D Anton and Chess】
2017-01-29 19:37
399 查看
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:
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”.
白子能否被吃掉,判断8方向上离白子最近的黑子是否可以吃掉白子
AC代码:
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”.
白子能否被吃掉,判断8方向上离白子最近的黑子是否可以吃掉白子
AC代码:
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const LL INF = 1LL << 63 - 1; LL dis[8],fx,fy; struct node{ char s[2]; LL x,y; }st[500010]; LL jl(LL x,LL y){ return (LL)(abs(x - fx) + abs(y - fy));} bool bc(LL x,LL y){ return (LL)(abs(x - fx)) == LL(abs(y - fy));} int main() { LL N,x,y; for(int i = 0; i <= 7 ; i++) dis[i] = INF; scanf("%lld %lld %lld",&N,&fx,&fy); for(int i = 1 ; i <= N; i++){ scanf("%s %lld %lld",st[i].s,&st[i].x,&st[i].y); LL x = st[i].x,y = st[i].y; if(x == fx && y > fy) dis[0] = min(dis[0],jl(x,y)); if(x == fx && y < fy) dis[1] = min(dis[1],jl(x,y)); if(y == fy && x < fx) dis[2] = min(dis[2],jl(x,y)); if(y == fy && x > fx) dis[3] = min(dis[3],jl(x,y)); if(bc(x,y)){ if(x < fx && y > fy) dis[4] = min(dis[4],jl(x,y)); if(x > fx && y > fy) dis[5] = min(dis[5],jl(x,y)); if(x > fx && y < fy) dis[6] = min(dis[6],jl(x,y)); if(x < fx && y < fy) dis[7] = min(dis[7],jl(x,y)); } } int ok = 0; for(int i = 1 ; i <= N; i++){ LL x = st[i].x,y = st[i].y; if(st[i].s[0] == 'R' || st[i].s[0] == 'Q'){ if(x == fx && y > fy && jl(x,y) == dis[0]) ok = 1; if(x == fx && y < fy && jl(x,y) == dis[1]) ok = 1; if(y == fy && x < fx && jl(x,y) == dis[2]) ok = 1; if(y == fy && x > fx && jl(x,y) == dis[3]) ok = 1; } if(((st[i].s[0] == 'B' || st[i].s[0] == 'Q')) && bc(x,y)){ if(x < fx && y > fy && jl(x,y) == dis[4]) ok = 1; if(x > fx && y > fy && jl(x,y) == dis[5]) ok = 1; if(x > fx && y < fy && jl(x,y) == dis[6]) ok = 1; if(x < fx && y < fy && jl(x,y) == dis[7]) ok = 1; } } if(ok) printf("YES\n"); else printf("NO\n"); return 0; }
相关文章推荐
- CodeForces 734D:Anton and Chess(模拟?)
- CodeForces 734 D.Anton and Chess(map)
- 【29.89%】【codeforces 734D】Anton and Chess
- codeforces 734 E. Anton and Tree (黑红树,缩点+dfs)
- CodeForces 734 A.Anton and Danik(水~)
- CodeForces 734 B.Anton and Digits(水~)
- codeforces 734 C. Anton and Making Potions (二分 STL)
- CodeForces 734 C.Anton and Making Potions(二分)
- CodeForces 734 E.Anton and Tree(dfs)
- Codeforces 785E Anton and Permutation(分块)
- CodeForces 785E Anton and Permutation 分块
- 【codeforces 508B】Anton and currency you all know
- Codeforces 559C Gerald and Giant Chess(DP+乘法逆元求大组合数)
- Vasya and Chess - CodeForces 493 D 博弈
- codeforces 560 E. Gerald and Giant Chess (dp+lucas定理,求大组合数 mod p,p为质数)
- 【13.77%】【codeforces 734C】Anton and Making Potions
- CodeForces 785A Anton and Polyhedrons
- codeforces 785 D. Anton and School - 2
- 【codeforces 785C】Anton and Fairy Tale
- 【81.37%】【codeforces 734B】Anton and Digits