Xiangqi(简单模拟)
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(简单模拟)的更多相关文章
- java web学习总结(二十二) -------------------简单模拟SpringMVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- WPF简单模拟QQ登录背景动画
介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...
- Linux 内核 链表 的简单模拟(2)
接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...
- Linux 内核 链表 的简单模拟(1)
第零章:扯扯淡 出一个有意思的题目:用一个宏定义FIND求一个结构体struct里某个变量相对struc的编移量,如 struct student { int a; //FIND(struct stu ...
- JavaWeb学习总结(四十九)——简单模拟Sping MVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- 简单模拟Hibernate的主要功能实现
在学习期间接触到Hibernate框架,这是一款非常优秀的O/R映射框架,大大简化了在开发web项目过程中对数据库的操作.这里就简单模拟其底层的实现. /*******代码部分,及其主要注解***** ...
- 【HDU 4452 Running Rabbits】简单模拟
两只兔子Tom和Jerry在一个n*n的格子区域跑,分别起始于(1,1)和(n,n),有各自的速度speed(格/小时).初始方向dir(E.N.W.S)和左转周期turn(小时/次). 各自每小时往 ...
- Jquery源码分析与简单模拟实现
前言 最近学习了一下jQuery源码,顺便总结一下,版本:v2.0.3 主要是通过简单模拟实现jQuery的封装/调用.选择器.类级别扩展等.加深对js/Jquery的理解. 正文 先来说问题: 1. ...
- (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数<=3,输出剩下的人 )
题目: 士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- NYOJ 题目77 开灯问题(简单模拟)
开灯问题 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 有n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2 ...
随机推荐
- ArcGIS为面要素生成邻接矩阵
1. 分析工具——>空间关联 使用注意,直接用FID似乎不可行,我是自己重新建了一个"String"字段,值用字段计算器从FID获取过来.之后按照上面的步骤才成功. 实现主要 ...
- Linux系统下的网络配置
一.修改配置文件,重启后设置不丢失 [Red Hat Linux/CentOS] 使用ifconfig查看使用的网口: [root@localhost /]# ifconfig 修改对应网口配置文件: ...
- cocos 水果机,老Tiger虎机流水灯,博彩大转盘效果
原(http://www.cnblogs.com/zisou/p/cocos2d-xZhuanpan.html) 博彩大转盘,转盘抽奖的小系统,这是一个很有意思的游戏模块,游戏中增加这样一些趣味的小模 ...
- python-爬虫(3)---lxml匹配css
百度首页 部分代码 <div class="s_tab_inner"> <b>网页</b> <a href="//www.ba ...
- 灵雀云获邀加入CDF(持续交付基金会),成为中国区三大创始成员之一
3月12日,在加州Half Moon Bay举行的开源领导者峰会(Open Leadership Summit 2019 )上,CDF(Continuous Delivery Foundation ) ...
- android 开发设计模式---Strategy模式
假设我们要出去旅游,而去旅游出行的方式有很多,有步行,有坐火车,有坐飞机等等.而如果不使用任何模式,我们的代码可能就是这样子的. 12345678910111213141516171819202122 ...
- 使用 dom4j 处理 xml (1)
解决问题需要,自己简单学习了一下dom4j 的基本用法: (1)读取 xml 文件: (2)修改 xml 文件. 需要的 jar 包: dom4j-xxx.jar (可以在 https://dom4j ...
- 关于Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇高质量的博文)
Java多线程的线程同步和线程通信的一些小问题(顺便分享几篇质量高的博文) 前言:在学习多线程时,遇到了一些问题,这里我将这些问题都分享出来,同时也分享了几篇其他博客主的博客,并且将我个人的理解也分享 ...
- cshtml razor
禁止转换字符 @(Html.Raw(@item.conent)) 三目运算 @(ViewBag.submitType==1?"blue":"")
- 用 webpack 创建 vue 项目
1.安装支持webpack 包 npm i -D webpack webpack-cli aspnet-webpack webpack-dev-middleware webpack-hot-mi ...