刘汝佳《算法竞赛入门经典(第二版)》习题(六)

刘汝佳《算法竞赛入门经典(第二版)》第四章习题(4-1~4-3)

习题4-1 象棋(Xiangai,ACM/ICPC Fuzhou 2011,UVa1589)

考虑一个象棋残局,其中红方有n(2≤n≤7)个旗子,黑方只有一个将。红方除了有一个帅(G)之外还有3种可能的棋子:车(R),马(H),炮(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 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 sleg”.

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.


样例输入和样例输出:



解析:

思路很简单,对于黑方将所有可行的下一步棋,红方的所有棋子都进行模拟攻击,根据攻击结果判断红方是否已经把黑方将死(或者按棋盘来进行搜索也可以AC,毕竟棋盘只有10行9列,应该不会超时,但明显是按棋子搜索更好,因为棋子最多只有7个)。思路不难但坑点很多:

1.小心棋盘越界。

2.将和帥的活动范围只有r行c列(1≤r≤3,4≤c≤6),对将的当前位置,对应的下一步位置以及路线数量都不同。

3.馬能否进行攻击的条件比较特殊,需要仔细考虑。

4.红方棋子相对于黑方将的位置不同,攻击路线也有所不同。

5.将与帥不能处在可以攻击对方的位置,否则红方必输。

(以为完了?想太多了…)

6.由于下一步是黑方走,因此要考虑到帥可能会吃掉红方(除将外)的棋子,而红方少一个棋子后不一定就将不死黑方(很多人死在这一点上)。

上述坑点都要考虑到,不然写的代码即使测试用例过了也还是会WA(或者拿不到满分)。

下面的代码并不是标准的比赛代码,因为虽然策略优,但是代码量较大,为了优化程序运行时间(其实这种思路对于这道题大可不必,我强迫症- -)添加了很多代码。主要看各函数的功能就行(代码注释讲得很清楚):

源代码:

#include <iostream>
#include <cctype>

using namespace std;

char chess[11][10];//棋盘

const int movH[8][2] = {
     {-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1}};//馬的走位

//棋子
struct Chess
{
    char type;
    int x;
    int y;
};

//由馬的走位推出“馬脚”的位置
int isMove (int i)
{
    if(i > 0)
        return -1;
    else if (i < 0)
        return 1;
    else
        return 0;
}

//帥的攻击路线
bool G (int x, int y, int target_x, int target_y)
{
    int i;
    if (y != target_y)//如果帥与目标不在同一纵轴上,则必定无法攻击成功
        return false;
    //将的攻击路线上是否有别的棋子
    for (i = x-1; i > target_x; i--)
        if (isalpha(chess[i][y]))
            return false;
    return true;
}

//車的攻击路线
bool R (int x, int y, int target_x, int target_y)
{
    int i;
    if (x != target_x &
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值