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 ...
随机推荐
- Appium IOS 使用多模拟器并发执行测试
申明一下 转载请注明出处 复制粘贴请滚蛋 !!!!!!!! 最近在是用appium进行app的并发测试,并且Android已经实现在同一台PC机使用多个模拟器并发测试的功能 这里说一句模拟器使 ...
- 【LeetCode每天一题】Add Binary(二进制加法)
Given two binary strings, return their sum (also a binary string).The input strings are both non-emp ...
- jenkins centos slave起不来报错The SSH key presented by the remote host does not match the key saved in the Known Hosts file against this host. Connections to this host will be denied until the two keys mat
场景:我的centos-204是一台centos的机器,本来用https://www.cnblogs.com/zndxall/p/8297356.html 的centos slave方式搭建ok的,一 ...
- python数组相关知识
1.np中的reshape函数,可以把矩阵重新划分成m行n列. arange(n)可以把 [0,n-1]装入数组中,一定要注意的是img.reshape()并不会改变原来的数组,所以需要另外新建一个数 ...
- unity组件路径自动生成
unity 有时候找路径太麻烦 写了一个自动生成脚本的工具 using System.Collections.Generic; using System.IO; using System.Text; ...
- ok6410如何从sdram中启动uboot 调试 这是一个猜想还没有验证
1\在smdk6410.h中定义宏 //#define CONFIG_SKIP_LOWLEVEL_INIT 1 //#define CONFIG_SKIP_RELOCATE_UBOOT 12\将编译的 ...
- 软件综合实践Axure介绍
首先就是下载安装Axure这款软件了,在百度上搜索“”Axure rp下载“”即可,下载完成后,打开exe安装,根据步骤一步步点击下一步即可完成安装. 运行该软件时会出现类似于填写激活码的东西,这时依 ...
- volatile关键字作用
1.阻止编译器对代码进行优化.即读取某个变量值时,不从寄存器中读取而是从变量里读. 2.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后 ...
- Winform 加载datagridview
string str = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True ...
- B/S架构图解