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

纯粹逻辑题目

思路:

先判断是否黑将直接能吃掉红将,这样黑方直接就赢了

再让黑将上下左右走,分别判断这样走安不安全

注意有些棋子可能直接能让黑将吃掉,还有越界问题

每种棋子注意判别将军方式,比如车要在同一条直线上,中间不能有棋子

炮的话,和将之间还需要一个棋子

马注意蹩脚马的情况

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <algorithm>

using namespace std;

struct One{
	int r, c;
	char type;
};

One Red[10];

int N, r0, c0, G_NO;
char tab[12][12]; // 棋盘
// 黑将能走的四个方向
const int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

// 马的行走方向
const int Hdir[8][4] = {
	{ -1, 2, 0, 2 }, { 1, 2, 0, 2 },
	{ -2, -1, -2, 0 }, { -2, 1, -2, 0 },
	{ -1, -2, 0, -2 }, { 1, -2, 0, -2 },
	{ 2, -1, 2, 0 }, { 2, 1, 2, 0 }
};

inline bool in_black_palace(const int r, const int c)
{
	return r >= 1 && r <= 3 && c >= 4 && c <= 6;
}

int get_range_block(int r1, int c1, int r2, int c2)
{
	int cnt = 0;
	if (r1 != r2 && c1 != c2) return -1;
	if (r1 == r2){
		if (c1 > c2) swap(c1, c2);
		for (int i = c1 + 1; i <= c2 - 1; ++i) {
			if (tab[r1][i] != '\0')
				cnt++;
		}
	}
	else if (c1 == c2){
		if (r1 > r2) swap(r1, r2);
		for (int i = r1 + 1; i <= r2 - 1; ++i) {
			if (tab[i][c1] != '\0') {
				cnt++;
			}
		}
	}
	return cnt;
}

// 四种棋子的将军判别方法
bool G(const int r, const int c, const int x, const int y)
{
	if (c != y) return false;
	return get_range_block(r, c, x, y) == 0;
}

bool R(const int r, const int c, const int x, const int y)
{
	int res = get_range_block(r, c, x, y);
	if (res == -1) return false;
	return res == 0;
}

bool H(const int r, const int c, const int x, const int y)
{
	for (int i = 0; i < 8; ++i) {
		int x1 = x + Hdir[i][0], y1 = y + Hdir[i][1];
		if (x1 == r && y1 == c && get_range_block(x, y, x + Hdir[i][2], y + Hdir[i][3]) == 0)
			return true;
	}
	return false;
}

bool C(const int r, const int c, const int x, const int y){
	int res = get_range_block(r, c, x, y);
	if (res == -1) {
		return false;
	}
	return res == 1;
}

bool check_red_win(const int r, const int c) {
	for (int i = 0; i < N; ++i) if (!(Red[i].r == r && Red[i].c == c)) {
		One & t = Red[i];
		if (t.type == 'G' && G(r, c, t.r, t.c)) return true;
		if (t.type == 'R' && R(r, c, t.r, t.c)) return true;
		if (t.type == 'H' && H(r, c, t.r, t.c)) return true;
		if (t.type == 'C' && C(r, c, t.r, t.c)) return true;
	}
	return false;
}

int main()
{
	//ios::sync_with_stdio(false);
	while ( cin >> N >> r0 >> c0 && (N != 0)) {
		// 记得清空红方棋子和棋盘
		memset(Red, 0, sizeof(Red)), memset(tab, 0, sizeof(tab));
		for (int i = 0; i < N; i++) {
			One t;
			cin >> t.type >> t.r >> t.c;
			if (t.type == 'G') {
				G_NO = i; // 标记黑将
			}
			tab[t.r][t.c] = t.type;
			Red[i] = t;
		}
		// 判断黑将能否直接吃掉红将
		if (G(r0, c0, Red[G_NO].r, Red[G_NO].r)) {
			printf("NO\n");
			continue;
		}
		bool red_win = true;
		// 红将向上下左右分别移动,看是否能逃脱
		for (int i = 0; i < 4; ++i) {
			int r1 = r0 + dir[i][0], c1 = c0 + dir[i][1];
			if (in_black_palace(r1, c1) && !check_red_win(r1, c1)) {
				red_win = false;
				break;
			}
		}
		if (red_win) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
	return 0;
}

Uva - 1589 - Xiangqi的更多相关文章

  1. ●UVa 1589 Xiangqi(模拟)

    ●赘述题意 给出一个中国象棋残局,告诉各个棋子的位置,黑方只有1枚“将”,红方有至少2枚,至多7枚棋子,包含1枚“帅G”,和若干枚“车R”,“马H”,“炮C”.当前为黑方的回合,问黑方的“将”能否在移 ...

  2. 【每日一题】 UVA - 1589 Xiangqi 函数+模拟 wa了两天

    题意:背景就是象棋, 题解:坑点1(wa的第一天):将军可以吃掉相邻的棋子,(然行列也写反了orz) 坑点2(wa的第二天):将军到马要反过来写,边界有误,并且第一次碰到的车才算(写到后来都忘了) # ...

  3. UVA 1589:Xiangqi (模拟 Grade D)

    题目: 象棋,黑棋只有将,红棋有帅车马炮.问是否死将. 思路: 对方将四个方向走一步,看看会不会被吃. 代码: 很难看……WA了很多发,还越界等等. #include <cstdio> # ...

  4. 【UVA】1589 Xiangqi(挖坑待填)

    题目 题目     分析 无力了,noip考完心力憔悴,想随便切道题却码了250line,而且还是错的,知道自己哪里错了,但特殊情况判起来太烦了,唯一选择是重构,我却没有这勇气. 有空再写吧,最近真的 ...

  5. uva 1589 by sixleaves

    坑爹的模拟题目.自己对于这种比较复杂点得模拟题的能力概述还不够,还多加练习.贴别是做得时候一直再想如何检查车中间有没有棋子,炮中间有没有棋子.到网上参考别人的代码才发先这么简单的办法,自己尽然想不到. ...

  6. UVA 1589 象棋

    题意: 给出一个黑方的将, 然后 红方有 2 ~ 7 个棋子, 给出摆放位置,问是否已经把黑将将死, 红方已经将军. 分析: 分情况处理, 车 马 炮, 红将情况跟车是一样的. 建一个数组board保 ...

  7. dir命令只显示文件名

    dir /b 就是ls -f的效果 1057 -- FILE MAPPING_web_archive.7z 2007 多校模拟 - Google Search_web_archive.7z 2083 ...

  8. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  9. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

随机推荐

  1. Delphi7.0常用函数-属性-事件

    abort 函数 引起放弃的意外处理 addexitproc 函数 将一过程添加到运行时库的结束过程表中 addr 函数 返回指定对象的地址 adjustlinebreaks 函数 将给定字符串的行分 ...

  2. Python中tuple的功能介绍

    Tuple的功能介绍 1. 元祖的两种方法 1. 元祖的内置方法 两个元祖的相加 格式:x.__add__(y)等同于x+y 例如:tu1 = (1,2,3,) print(tu1.__add__(( ...

  3. 谷歌开发者:看可口可乐公司是怎么玩转TensorFlow的?

    在这篇客座文章中,可口可乐公司的 Patrick Brandt 将向我们介绍他们如何使用 AI 和 TensorFlow 实现无缝式购买凭证. 可口可乐的核心忠诚度计划于 2006 年以 MyCoke ...

  4. Zend引擎探索 之 PHP中前置递增不返回左值

    首先来讲,一般我们对"左值"的理解就是可以出现在赋值运算符的左侧的标识符,也就是可以被赋值.这样讲也许并不十分确切,在不同的语言中对左值的定义也不尽相同.在这里我们讨论前置递增(和 ...

  5. JVM程序计数器

    一.先来看看概念 多线程的Java应用程序:为了让每个线程正常工作就提出了程序计数器(Programe Counter Register),每个线程都有自己的程序计数器这样当线程执行切换的时候就可以在 ...

  6. 简单的国际化i18n

    就是简单的中英文转换 index.jsp <%@ page language="java" contentType="text/html; charset=UTF- ...

  7. linux上安装fastdfs+nginx+ngin-module实践并解决多个异常篇

    为什么选择Nginx Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性: 在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主 ...

  8. 110个oracle常用函数总结

    . ASCII 返回与指定的字符对应的十进制数; SQL) zero,ascii( ) space from dual; A A ZERO SPACE --------- --------- ---- ...

  9. PyCharm 2018.1破解过程

    一.下载 首先从官网下载 官网,如果开了酸酸乳的话无法下载,官网会自动断开连接.所以下载时请关闭酸酸乳 二.安装 选择安装路径 选择64位,创建关联.py文件 安装完后运行Pycharm 选择不导入开 ...

  10. ACM 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    Problem Description 急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品, ...