象棋(Xiangqi, ACM/ICPC Fuzhou 2011, UVa1589)C++超详细解题

象棋(Xiangqi, ACM/ICPC Fuzhou 2011, UVa1589)C++超详细解题

象棋(Xiangqi, ACM/ICPC Fuzhou 2011, UVa1589)C++超详细解题

题目

Description

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 enemy 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 “checkmate”.

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 right figure). “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 left figure), which is called “hobbling the horse’s leg”.

在这里插入图片描述
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’.
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
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 above.

在这里插入图片描述
Situation 1
在这里插入图片描述
Situation 2

思路

  1. 题目保证将军状态,不用考虑当前情况
  2. 只需要判断黑将的4个移动位置是不是都被封死,判断方法就是模拟黑将移动,看是否仍是被将军状态
  3. 循环到黑将的位置直接跳过,就不用考虑移动时黑将吃红子的情况,将黑将的位子记录下来,不需要把黑将放在棋盘里,即整个棋盘一直保持不变。

代码

#include <math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char map[15][15];//存储棋盘,每个红棋子都存入
int  b_x, b_y, flag = 0;//黑将的位置,死亡标记
int mov[4][2] = { {-1,0},{1,0},{0,1},{0,-1} };//黑将的4中可能的移动

void make_G(int x, int y) {//判断红将能否杀死黑将
如果红将和黑将之间没有其他子,杀死黑将
	if (abs(y - b_y) != 0)
		return;
	else
		for (int i = b_x + 1;i <= x - 1;i++)
			if (map[i][y] != '#')
				return;
	flag = 1;
}
void make_R(int x, int y) {//判断红车能否杀死黑将
	if (abs(y - b_y) != 0 && abs(x - b_x) != 0)
		return;
	int flag1 = 0, flag2 = 0;
	//如果红车和黑将之间没有其他子,杀死黑将
	if (abs(y - b_y) == 0) {
		for (int i = min(x, b_x)+1;i < max(x, b_x);i++)
			if (map[i][y] != '#')
				flag1 = 1;
		if (flag1 == 0)
			flag = 1;
	}
	if (abs(x - b_x) == 0) {
		for (int i = min(y, b_y)+1;i < max(y, b_y);i++)
			if (map[x][i] != '#')
				flag2 = 1;
		if (flag2 == 0)
			flag = 1;
	}
}
void make_H(int x, int y) {//判断红马能否杀死黑将
//将红马能走的位置存进数组,遍历是否等于黑将位子,等则杀死
	int tep[10][2],index=0;
	memset(tep, 0, sizeof(tep));
	if (abs(x - b_x) > 2 || (abs(y - b_y) > 2))
		return;
	else {
		if (map[x - 1][y] == '#') {
			tep[index][0] = x - 2;
			tep[index++][1] = y - 1;
			tep[index][0] = x - 2;
			tep[index++][1] = y + 1;
		}
		if (map[x + 1][y] == '#') {
			tep[index][0] = x + 2;
			tep[index++][1] = y - 1;
			tep[index][0] = x + 2;
			tep[index++][1] = y + 1;
		}
		if (map[x][y + 1] == '#') {
			tep[index][0] = x - 1;
			tep[index++][1] = y + 2;
			tep[index][0] = x + 1;
			tep[index++][1] = y + 2;
		}
		if (map[x][y - 1] == '#') {
			tep[index][0] = x - 1;
			tep[index++][1] = y - 2;
			tep[index][0] = x + 1;
			tep[index++][1] = y - 2;
		}
	}
	for (int i = 0;i < 10;i++) {
		if (tep[i][0] == b_x && tep[i][1] == b_y) {
			flag = 1;
			return;
		}
	}
}
void make_C(int x, int y) {//判断红炮能否杀死黑将
//判断红炮和黑将之间有几子,1个则杀死黑将
	if (abs(y - b_y) != 0 && abs(x - b_x) != 0)
		return;
	int count = 0;
	if (abs(y - b_y) == 0) {
		for (int i = min(x, b_x)+1;i < max(x, b_x);i++)
			if (map[i][y] != '#')
				count++;
	}
	if (abs(x - b_x) == 0) {
		for (int i = min(y, b_y)+1;i < max(y, b_y);i++)
			if (map[x][i] != '#')
				count++;
	}
	if (count == 1)
		flag = 1;
}
int main() {
	int n,r_x,r_y,x,y,num=0;//num代表黑将的4个移动位置死亡的次数
	char type;
	while (scanf(" %d %d %d", &n, &x, &y) == 3 && n != 0) {
		num = 0;
		memset(map, '#', sizeof(map));
		for (int i = 0;i < n;i++) {
			scanf(" %c %d %d", &type, &r_x, &r_y);
			map[r_x][r_y] = type;
		}
		for (int p = 0;p < 4;p++) {//遍历黑将的4种移动
			flag = 0;
			b_x = x + mov[p][0];
			b_y = y + mov[p][1];
			if (b_x > 3 || b_x < 1 || b_y>6 || b_y < 4) {
				num++;//如果黑将越界,直接当做死亡
				continue;
			}
			for (int i = 1;i <= 10;i++) {
				for (int j = 1;j <= 9;j++) {
					if (b_x == i && b_y == j)
						continue;//模拟黑将吃子,跳过此子即可
					switch (map[i][j]) {
					case 'G':
						make_G(i, j);
						break;
					case 'R':
						make_R(i, j);
						break;
					case 'C':
						make_C(i, j);
						break;
					case 'H':
						make_H(i, j);
						break;
					default:
						break;
					}
				}
			}
			if (flag == 1)
				num++;//如果有红子杀死黑将,死亡加一
		}
		if (num == 4)//四个位置全死,将死
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值