4746: Xiangqi

时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

总提交: 15            测试通过:2

描述

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’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”.

输入

The input contains multiple 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.

输出

For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.

样例输入

2 1 4
G 10 5
R 6 4

3 1 5
H 4 5
G 10 5
C 7 5

0 0 0

样例输出

YES
NO

提示

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.

题意:黑方只有一枚将棋,红方则有多枚棋子,此时黑方动,判断黑方是否已经无路可走,即被将死。

题解:模拟判断红方棋子的走向,再判断黑将4个方向移动是否能被红方的棋子所将死。

 #include "iostream"
#include "string.h"
using namespace std;
int weizhi[][];
int wxl(int a,int b)
{
int i,flag;//flag判断此位置是否有棋子和有几个棋子
// 判断马的走位
if((a-)>&&(b-)>&&weizhi[a-][b-]==&&!weizhi[a-][b-])return ;
if((a-)>&&(b+)<=&&weizhi[a-][b+]==&&!weizhi[a-][b+])return ;
if((a-)>&&(b-)>&&weizhi[a-][b-]==&&!weizhi[a-][b-])return ;
if((a+)<=&&(b-)>&&weizhi[a+][b-]==&&!weizhi[a+][b-])return ;
if((a+)<=&&(b-)>&&weizhi[a+][b-]==&&!weizhi[a+][b-])return ;
if((a+)<=&&(b+)<=&&weizhi[a+][b+]==&&!weizhi[a+][b+])return ;
if((a-)>&&(b+)<=&&weizhi[a-][b+]==&&!weizhi[a-][b+])return ;
if((a+)<=&&(b+)<=&&weizhi[a+][b+]==&&!weizhi[a+][b+])return ; // 判断炮和车和将和帅是否直接对面
flag=;
for(i=a-;i>;i--)
{
if(!flag&&weizhi[i][b]==||weizhi[i][b]==)return ;
if(weizhi[i][b]==&&flag==)return ;
if(weizhi[i][b]!=)flag++;//判断炮有几个跳跃点
}
flag=;
for(i=a+;i<=;i++)
{
if(!flag&&weizhi[i][b]==||weizhi[i][b]==)return ;
if(weizhi[i][b]==&&flag==)return ;
if(weizhi[i][b]!=)flag++;
}
flag=;
for(i=b-;i>;i--)
{
if(!flag&&weizhi[a][i]==||weizhi[a][i]==)return ;
if(weizhi[a][i]==&&flag==)return ;
if(weizhi[a][i]!=)flag++;
}
flag=;
for(i=b+;i<;i++)
{
if(!flag&&weizhi[a][i]==||weizhi[a][i]==)return ;
if(weizhi[a][i]==&&flag==)return ;
if(weizhi[a][i]!=)flag++;
}
return ;//黑将不会被将军
} int main()
{
int i,k,n,m,b,c,flag;
char a;
while(cin>>n>>m>>k)
{
if(n==&&m==&&k==)break;
memset(weizhi,,sizeof weizhi);//给位置初始化为0
for(i=;i<n;i++)
{
cin>>a>>b>>c;
if(a=='G')weizhi[b][c]=;//4表示这个点的棋子是帅
if(a=='H')weizhi[b][c]=;//5表示这个点的棋子是马
if(a=='C')weizhi[b][c]=;//2表示这个点的棋子是炮
if(a=='R')weizhi[b][c]=;//3表示这个点的棋子是车
}
//判断将的4个移动方向是否还会被将军
if(m+<=&&m+>&&k>=&&k<=)//因为黑将的移动是有范围的,所以不能越界
{
flag=wxl(m+,k);
if(flag)
{
cout<<"NO"<<'\n';continue;
}
}
if(m-<=&&m->&&k>=&&k<=)
{
flag=wxl(m-,k);
if(flag)
{
cout<<"NO"<<'\n';continue;
}
}
if(m<=&&m>&&k+>=&&k+<=)
{
flag=wxl(m,k+);
if(flag)
{
cout<<"NO"<<'\n';continue;
}
}
if(m<=&&m>&&k->=&&k-<=)
{
flag=wxl(m,k-);
if(flag)
{
cout<<"NO"<<'\n';continue;
}
}
cout<<"YES"<<'\n';
}
}
//但是TOJ有一点不用考虑,我也没考虑,其他oj则需要考虑,就是红方是用帅来将军的,这是轮到黑方
//移子,那么应该是黑方胜,而TOJ则还是红方胜

Xiangqi(简单模拟)的更多相关文章

  1. java web学习总结(二十二) -------------------简单模拟SpringMVC

    在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...

  2. WPF简单模拟QQ登录背景动画

    介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...

  3. Linux 内核 链表 的简单模拟(2)

    接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...

  4. Linux 内核 链表 的简单模拟(1)

    第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...

  5. JavaWeb学习总结(四十九)——简单模拟Sping MVC

    在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...

  6. 简单模拟Hibernate的主要功能实现

    在学习期间接触到Hibernate框架,这是一款非常优秀的O/R映射框架,大大简化了在开发web项目过程中对数据库的操作.这里就简单模拟其底层的实现. /*******代码部分,及其主要注解***** ...

  7. 【HDU 4452 Running Rabbits】简单模拟

    两只兔子Tom和Jerry在一个n*n的格子区域跑,分别起始于(1,1)和(n,n),有各自的速度speed(格/小时).初始方向dir(E.N.W.S)和左转周期turn(小时/次). 各自每小时往 ...

  8. Jquery源码分析与简单模拟实现

    前言 最近学习了一下jQuery源码,顺便总结一下,版本:v2.0.3 主要是通过简单模拟实现jQuery的封装/调用.选择器.类级别扩展等.加深对js/Jquery的理解. 正文 先来说问题: 1. ...

  9. (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数&lt;=3,输出剩下的人 )

    题目: 士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  10. NYOJ 题目77 开灯问题(简单模拟)

    开灯问题 时间限制:3000 ms  |            内存限制:65535 KB 难度:1           描述 有n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2 ...

随机推荐

  1. Extjs6 modern formpanel 上传文件 问题

    要设置 enableSubmissionForm: false 否则chrome会报 Form submission canceled because the form is not connecte ...

  2. Nginx配置不当可能导致的安全问题

    Nginx配置不当可能导致的安全问题 Auther: Spark1e目前很多网站使用了nginx或者tenginx(淘宝基于Nginx研发的web服务器)来做反向代理和静态服务器,ningx的配置文件 ...

  3. node中处理异步常用的方法,回调函数和events 模块处理异步

    // npm install -g supervisor supervisor http.js就可以实现热更新的效果 //引入http模块 var http = require('http'); va ...

  4. 利用FPN构建Faster R-CNN检测

    FPN就是所谓的金字塔结构的检测器,(Feature Pyramid Network) 把FPN融合到Faster rcnn中能够很大程度增加检测器对全图信息的认知, 步骤如图所示: 1.先将图像送入 ...

  5. Apache2 httpd.conf配置文件中文版详解

    Apache2 httpd.conf配置文件中文版详解## 基于 NCSA 服务的配置文件.##这是Apache服务器主要配置文件.#它包含服务器的影响服务器运行的配置指令.#参见以取得关于这些指令的 ...

  6. appium 3 跑起来

    1. 代码如下: from appium import webdriver capabilitise = { "platformName": "Android" ...

  7. java快排思想

    1分治思想 1.1比大小在分区 1.2从数组中取出一个数做基准数 1.3将比他小的数全放在他的左边,比他大的数全放在他的右边 1.4然后递归 左边 和右边 }

  8. Python语言——Python语言概述

    Python语言概述 计算机语言概述 语言:交流工具,沟通媒介 计算机语言:人和计算机交流的工具,翻译官 Python语言简述 Python是计算机语言的一种 Python编程语言: 代码:人类语言, ...

  9. js点击出现二级菜单,点击二级菜单主菜单换成二级菜单

    点击出现二级菜单 *{ margin:0px auto; padding:0px; } .yiji{ width:200px; height:40px; background-color:red; c ...

  10. pycharm的断点调试与TODO标记

    断点调试的方法: 断点调试在程序比较大的时候调试运用的比较多 点击Pycharm软件右上角绿色三角形右边的小甲鱼图标,点击之后会弹出断点调试的界面 Debug是用来调试bug的 terminal 是终 ...