Xiangqi is one of the most popular two-player board games in China. The game represents a battle
between two armies with the goal of capturing the enemy’s “general” piece. In this problem, you are
given a situation of later stage in the game. Besides, the red side has already “delivered a
check”. Your work is to check whether the situation is “checkmate”.
Now we introduce some basic rules of Xiangqi. Xiangqi is played on a 10 × 9 board and the pieces
are placed on the intersections (points). The top left point is (1,1) and the bottom right point is
(10,9). There are two groups of pieces marked by black or red Chinese characters, belonging to the
two players separately. During the game, each player in turn moves one piece from the point it
occupies to another point. No two pieces can occupy the same point at the same time. A piece can be
moved onto a point occupied by an enemy piece, in which case the enemy piece is“captured” and
removed from the board. When the general is in danger of being captured by the enemy player on the
en- emy player’s next move, the enemy player is said to have “delivered a check”. If the general’s
player can make no move to prevent the general’s capture by next enemy move, the situation is
called “check-
mate”.
We only use 4 kinds of pieces introducing as follows:
General: the generals can move and capture one point either vertically or horizontally and cannot
leave the “palace” unless the situation called “flying general” (see the figure above). “Flying
general” means that one general can “fly” across the board to capture the enemy general if they
stand on the same line without intervening pieces.
Chariot: the chariots can move and capture vertically and horizontally by any distance, but may not
jump over intervening pieces
Cannon: the cannons move like the chariots, horizontally and vertically, but capture by jumping
exactly one piece (whether it is friendly or enemy) over to its target.
Horse: the horses have 8 kinds of jumps to move and capture shown in the left figure. However, if
there is any pieces lying on a point away from the horse horizontally or vertically it cannot move
or capture in that direction (see the figure below), which is called “hobbling the
horse’s leg”.
Now you are given a situation only containing a black general, a red general and several red
chariots, cannons and horses, and the red side has delivered a check. Now it turns to black side’s
move. Your job is to determine that whether this situation is “checkmate”.
Input
The input contains no more than 40 test cases. For each test case, the first line contains three
integers representing the number of red pieces N (2 ≤ N ≤ 7) and the position of the black general.
The following N lines contain details of N red pieces. For each line, there are a char and two
integers representing the type and position of the piece (type char ‘G’ for general, ‘R’ for
chariot, ‘H’ for horse and ‘C’ for cannon). We guarantee that the situation is legal and the red
side has delivered the check.
There is a blank line between two test cases. The input ends by ‘0 0 0’.
Output
For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the
word ‘NO’.
Hint: In the first situation, the black general is checked by chariot and “flying general”. In the
second situation, the black general can move to (1, 4) or (1, 6) to stop check. See the figure on
the right.
Sample Input
2 1 4
G 10 5
R 6 4
3 1 5
H 4 5
G 10 5
C 7 5
0 0 0
Sample Output
YES
NO
注意:首先判断黑将和红将是否直接照面,如果是,则黑将可以吃掉红将,此时不将军
然后对黑将可能走的每一步进行遍历,此时要注意黑将有可能吃掉路上的棋子,然后对每一个红棋遍历,如果红棋无法到达则输出"NO"
越界必须要注意,棋子是否走在棋盘上,以及将能否出格子,对于将还需要注意斜线的走法
马的蹩脚需要注意,即马走每一步需要判断是否有棋子在路上
炮和车需要注意路上是否存在其他棋子
这份代码暂时还是WA状态,如果后续达到AC会更新代码,暂时正在debug阶段,还没能找出错误
//
// 1589.cpp
// oj
//
// Created by faramita on 18/4/26.
// Copyright © 2018年 faramita. All rights reserved.
//
#include <stdio.h>
#include <iostream>
using namespace std;
int n,x,y;
int b[10+1][9+1];
struct point{
char t;
int xx;
int yy;
};
point a[7];
int h[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};//马的走法
int g[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};//将的走法
bool range(int x1,int y1){//判断是否越界,走出棋盘
if(x1<=10&&x1>=1&&y1>=1&&y1<=9)return true;
return false;
}
void print(){//输出打印验证
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
cout<<b[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
bool horse_range(int x1,int y1,int i){//马的越界
if(i==0||i==1){
x1=x1-1;
if(range(x1, y1)){
if(b[x1][y1]!=0){
return false;
}
}else{
return false;
}
}else
if(i==2||i==3){
y1=y1+1;
if(range(x1, y1)){
if(b[x1][y1]!=0){
return false;
}
}else{
return false;
}
}else
if(i==4||i==5){
x1=x1+1;
if(range(x1, y1)){
if(b[x1][y1]!=0){
return false;
}
}else{
return false;
}
}else
if(i==6||i==7){
y1=y1-1;
if(range(x1, y1)){
if(b[x1][y1]!=0){
return false;
}
}else{
return false;
}
}
return true;
}
bool b_range(int x1,int y1){//是否在米字格中
if(x1<=3&&x1>=1&&y1>=4&&y1<=6)return true;
else return false;
}
bool black_range(int x1,int y1,int x2,int y2){//判断是否在黑将的米字格子中
if(x2==2&&y2==5){
return b_range(x1,y1);
}else{
if((x2==1&&y2==5)||(x2==2&&y2==4)||(x2==3&&y2==5)||(x2==2&&y2==6)){
if((x2==1&&y2==5)||(x2==3&&y2==5)){
if(x1==2&&(y1==4||y1==6)){
return false;
}else if(b_range(x1, y1)){
return true;
}else return false;
}else{
if(y1==5&&(x1==3||x1==1)){
return false;
}else if(b_range(x1, y1)){
return true;
}else return false;
}
}else{
return b_range(x1, y1);
}
}
return b_range(x1, y1);
}
void horse(int x1,int y1){//马走
int x2,y2;
int x3,y3;
for(int i=0;i<n;i++){
if(a[i].t=='H'){
x2=a[i].xx;
y2=a[i].yy;
x3=a[i].xx;
y3=a[i].yy;
for(int j=0;j<8;j++){
if(horse_range(x3,y3,j)){
x2=x3+h[j][0];
y2=y3+h[j][1];
if(range(x2, y2)&&x1==x2&&y1==y2){
b[x2][y2]=-1;
break;
}
}
}
}
}
}
void general(int x1,int y1){//红将
bool flag=false;
for(int j=0;j<n;j++){
flag=false;
if(a[j].t=='G'&&a[j].yy==y1){
for(int i=x1+1;i<a[j].xx;i++){
if(b[i][y1]!=0&&b[i][y1]!=-1)
{
flag=true;
break;
}
}
if(!flag){
b[x1][y1]=-1;
}
return;
}
}
}
void c(int x1,int y1){//炮
int x2,y2;
for(int i=0;i<n;i++){
if(a[i].t=='C'){
x2=a[i].xx;
y2=a[i].yy;
if(x1==x2){
int count=0;
if(y2<y1){
for(int j=y2+1;j<y1;j++){
if(b[x1][j]!=0&&b[x1][j]!=-1){
count++;
}
}
}else{
for(int j=y1+1;j<y2;j++){
if(b[x1][j]!=0&&b[x1][j]!=-1){
count++;
}
}
}
if(count==1){
b[x1][y1]=-1;
}
}else if(y1==y2){
int count=0;
if(x1<x2){
for(int j=x2-1;j>x1;j--){
if(b[j][y1]!=0&&b[j][y1]!=-1){
count++;
}
}
}else{
for(int j=x1-1;j>x2;j--){
if(b[j][y1]!=0&&b[j][y1]!=-1){
count++;
}
}
}
if(count==1){
b[x1][y1]=-1;
}
}
}
}
}
void r(int x1,int y1){//车
int x2,y2;
for(int i=0;i<n;i++){
if(a[i].t=='R'){
x2=a[i].xx;
y2=a[i].yy;
if(x1==x2){
int count=0;
if(y2<y1){
for(int j=y2+1;j<y1;j++){
if(b[x1][j]!=0&&b[x1][j]!=-1){
count++;
}
}
}else{
for(int j=y1+1;j<y2;j++){
if(b[x1][j]!=0&&b[x1][j]!=-1){
count++;
}
}
}
if(count==0){
b[x1][y1]=-1;
}
}else if(y1==y2){
int count=0;
if(x1<x2){
for(int j=x2-1;j>x1;j--){
if(b[j][y1]!=0&&b[j][y1]!=-1){
count++;
}
}
}else{
for(int j=x1-1;j>x2;j--){
if(b[j][y1]!=0&&b[j][y1]!=-1){
count++;
}
}
}
if(count==0){
b[x1][y1]=-1;
}
}
}
}
}
void set(){//最后判断是否有路可走
int x1=x,y1=y;
for(int i=0;i<8;i++){
x1=x+g[i][0];
y1=y+g[i][1];
if(black_range(x1, y1,x,y)&&b[x1][y1]==0){
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
return;
}
bool re(){//判断是否直接吃掉将
bool flag=false;
for(int j=0;j<n;j++){
if(a[j].t=='G'&&a[j].yy==y){
for(int i=x+1;i<a[j].xx;i++){
if(b[i][y]!=0&&b[i][y]!=-1)
{
flag=true;
break;
}
}
if(!flag){
cout<<"NO"<<endl;
return true;
}
return false;
}
}
return false;
}
void test(){//对于黑将的每一步进行判断
int x1=x,y1=y;
int tmp=0;
for(int i=0;i<8;i++){
x1=x+g[i][0];
y1=y+g[i][1];
if(black_range(x1, y1,x,y)){
if(b[x1][y1]!=-1){
tmp=b[x1][y1];
b[x1][y1]=0;
switch (tmp) {//如果吃掉棋子,则该棋子无法行走
case 2:
r(x1,y1);
c(x1,y1);
general(x1, y1);
break;
case 3:
r(x1,y1);
horse(x1, y1);
general(x1, y1);
break;
case 4:
c(x1,y1);
horse(x1, y1);
general(x1, y1);
break;
default:
r(x1,y1);
c(x1,y1);
horse(x1, y1);
general(x1, y1);
break;
}
if(b[x1][y1]!=-1)//是否直接照面
{
cout<<"NO"<<endl;
return;
}
}
}
}
set();
}
int main(){
cin>>n>>x>>y;
while(n!=0){
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
b[i][j]=0;
}
}
for(int i=0;i<n;i++){
int x1,y1;
char t;
cin>>t>>x1>>y1;
point tmp;
tmp.t=t;
tmp.xx=x1;
tmp.yy=y1;
a[i]=tmp;
switch (t) {//存棋子
case 'G':
b[x1][y1]=1;
break;
case 'H':
b[x1][y1]=2;
break;
case 'C':
b[x1][y1]=3;
break;
case 'R':
b[x1][y1]=4;
break;
}
}
if(!re()){
test();
}
cin>>n>>x>>y;
}
return 0;
}