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 ...
随机推荐
- np.mgrid 用法
import numpy as np dtype=np.float32 num_anchors = 6 y, x = np.mgrid[0:4, 0:4] print(y) print(x) y = ...
- *** target pattern contains no `%'. Stop.
windows上的ndk工程移到linux下编译就报这个错, 解决方法,把jni目录下obj目录删除,重新编译就好了
- day1:java学习第一天之eclipse安装
选择开发语言的学习其实不用纠结,如果你说自己是做开发的,连最流行的开发语言都不会,好像说不过去,并且最流行也说明用的人多,优秀的人也会,自己要提高要多向优秀的人学习.想明白这点其实选择就好说了,再一个 ...
- 前端学习历程--vue
---恢复内容开始--- 一.对比其他框架 1.react: 共同点: 使用 Virtual DOM 提供了响应式(Reactive)和组件化(Composable)的视图组件. 将注意力集中保持在核 ...
- Django框架详细介绍---中间件(认证)
一.绪论 在cookie和session的应用中,通过在视图函数内添加装饰器判断用户是否登录,把没有登录的用户请求跳转到登录页面,通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可 ...
- 理解linux网络管理命令
linux 管理命令,iproute 查看帮助文件: man ip 以下为常用帮助文件. SEE ALSO ip-address(), ip-addrlabel(), ip-l2tp(), ip-li ...
- CentOS7.3安装zimbra8.7.11
系统版本:CentOS7.3 Zimbra版本:8.7.11 设置IP地址 vi /etc/sysconfig/network-scripts/ifcfg-ens33 #使用vi命令修改ip BOOT ...
- 2018-2019-2 网络对抗技术 20165335 Exp3 免杀原理与实践
一.免杀原理与基础知识: (1)杀软是如何检测出恶意代码的? 检测特征码:特征码就是一般程序都不会有的代码,而后门有的那种特别的数据,而一个程序,应用有这种代码,数据的话,就直接判定为恶意代码. 主流 ...
- python函数部分----函数初识
0.来源http://www.cnblogs.com/jin-xin/articles/8241942.html 1.return 返回0个返回值,返回一个返回值.返回多个返回值 None.如果一个变 ...
- C++中的string类型转换为int类型
给定一个十进制整数n,输出n的各位数字之和 #include<iostream> #include<string> using namespace std; int main( ...