描述

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.

题意

黑方只右1个将,红方有车马炮帅,当前黑方走,判断红方是否已经将死黑方

题解

模拟将能走的4个点,判断这个点是否被红子杀

代码

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
int Map1[][],Map2[][],Dead[][];//Map1原来的棋盘,2将移动后的棋盘
void Up1(int x,int y)//帅
{
for(int i=x-;i>=;i--)//往上找
{
if(Map2[i][y]>=)//遇到阻拦
break;
Dead[i][y]=;//帅杀死的区域
}
}
void Up2(int x,int y)//车
{
for(int i=x-;i>=;i--)//往上找
{
if(Map2[i][y]>=)//遇到阻拦
break;
Dead[i][y]=;//车杀死
}
for(int i=x+;i<=;i++)//下
{
if(Map2[i][y]>=)
break;
Dead[i][y]=;
}
for(int j=y-;j>=;j--)//左
{
if(Map2[x][j]>=)
break;
Dead[x][j]=;
}
for(int j=y+;j<=;j++)//右
{
if(Map2[x][j]>=)
break;
Dead[x][j]=;
}
}
int dx[]={,,-,};//右,左,下,上
int dy[]={,-,,};
int ddx[]={-,,-,,-,-,,};
int ddy[]={,,-,-,-,,-,};
bool bj3(int i,int j)//马边界
{
if(i>=&&i<=&&j>=&&j<=)
return true;
return false;
}
void Up3(int x,int y)//马
{
for(int i=;i<;i++)
{
//马脚
if(Map2[x+dx[i]][y+dy[i]]>=)continue;
//马杀死
if(bj3(x+ddx[*i],y+ddy[*i]))Dead[x+ddx[*i]][y+ddy[*i]]=;
if(bj3(x+ddx[*i+],y+ddy[*i+]))Dead[x+ddx[*i+]][y+ddy[*i+]]=; }
}
void Up4(int x,int y)//跑
{
int F=;
for(int i=x-;i>=;i--)//上
{
if(Map2[i][y]>=)
F++;
if(F==)//架炮
Dead[i][y]=;
if(F==)//阻拦
break;
}
F=;
for(int i=x+;i<=;i++)//下
{
if(Map2[i][y]>=)
F++;
if(F==)
Dead[i][y]=;
if(F==)
break;
}
F=;
for(int j=y-;j>=;j--)//左
{
if(Map2[x][j]>=)
F++;
if(F==)
Dead[x][j]=;
if(F==)
break;
}
F=;
for(int j=y+;j<=;j++)//右
{
if(Map2[x][j]>=)
F++;
if(F==)
Dead[x][j]=;
if(F==)
break;
}
}
bool bj(int i,int j)//将边界
{
if(<=i&&i<=&&<=j&&j<=)
return true;
return false;
}
void Clear()
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
Map2[i][j]=Map1[i][j],Dead[i][j]=;
}
void Updata()
{
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
if(Map2[i][j]==)//帅
Up1(i,j);
if(Map2[i][j]==)//车
Up2(i,j);
if(Map2[i][j]==)//马
Up3(i,j);
if(Map2[i][j]==)//炮
Up4(i,j);
}
}
}
int main()
{
int n,a,b,hx,hy,gx,gy;
char c;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d%d",&n,&hx,&hy)!=EOF,n||hx||hy)
{
memset(Map1,,sizeof(Map1));
for(int i=;i<=n;i++)
{
c=getchar();
while(c==' ')c=getchar();//不加会错,估计卡空格了
scanf("%c %d %d",&c,&a,&b);
if(c=='G')//帅
Map1[a][b]=,gx=a,gy=b;
if(c=='R')//车
Map1[a][b]=;
if(c=='H')//马
Map1[a][b]=;
if(c=='C')//炮
Map1[a][b]=;
}
int win=;
//黑方直接飞将杀死红方(TOJ上是不考虑,其他平台要考虑)
//题意没说清,按残局来说的话这样毫无意义
/*
if(hy==gy)
{
int F=1;
for(int i=gx-1;i>=hx;i--)
{
if(Map1[i][gy]>=1)
{
F=0;break;
}
}
if(F)
{
printf("NO\n");
continue;
}
}
*/
for(int i=;i<;i++)
{
if(!bj(hx+dx[i],hy+dy[i]))continue;
Clear();
Map2[hx+dx[i]][hy+dy[i]]=;//吃子
Updata();
if(Dead[hx+dx[i]][hy+dy[i]])
{
win=;break;
}
}
if(win)printf("YES\n");
else printf("NO\n");
}
return ;
}

TZOJ 4746 Xiangqi(模拟棋盘数组)的更多相关文章

  1. TZOJ 4813 机器翻译(模拟数组头和尾)

    描述 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先在内存中查找这 ...

  2. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

  3. HDU 4121 Xiangqi 模拟题

    Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...

  4. Clumsy Keke【模拟+三维数组】

    Clumsy Keke 题目链接(点击) Problem Description Keke is currently studying engineering drawing courses, and ...

  5. JS模拟实现数组的map方法

    昨天使用map方法的时候,突然感觉一直在直接用,也没有试试是怎么实现的,本来想直接搜一篇文章盘一下子,结果没搜到合适的,好吧,那就自己来写一下子吧 今天就来实现一个简单的map方法 首先我们来看一下m ...

  6. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  7. TZOJ 5280 搜索引擎(模拟字符串)

    描述 谷歌.百度等搜索引擎已经成为了互连网中不可或缺的一部分.在本题中,你的任务也是设计一个搜索论文的搜索引擎,当然,本题的要求比起实际的需求要少了许多. 本题的输入将首先给出一系列的论文,对于每篇论 ...

  8. ACM-ICPC北京赛区(2017)网络赛1【模拟+枚举+数组操作】

    题目1 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n ...

  9. hdu4121 poj4001 Xiangqi(模拟)

    模拟题考验coding能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了.高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明. 方法选择的好坏 ...

随机推荐

  1. red hat官方的rhel操作系统版本号与内核版本号的对应关系

    原文在如下网址:https://access.redhat.com/articles/3078 The tables below list the major and minor Red Hat En ...

  2. android手机版本

    1.判断安卓手机是64位,还是32位 adb pull /system/bin/cat file cat 32位 64位 2.判断安卓手机CPU是64位,还是32位 adb shell getprop ...

  3. Java 默认事务级别read committed对binlog_format的需求

    转载: java.sql.SQLException: Cannot execute statement: impossible to write to binary log since BINLOG_ ...

  4. linux下一个网卡配置多个ip【虚拟ip】

    Linux下配置网卡ip别名何谓ip别名?用windows的话说,就是为一个网卡配置多个ip.什么场合增加ip别名能派上用场?布网需要.多ip访问测试.特定软件对多ip的需要...and so on. ...

  5. 成为Java顶尖程序员 ,看这11本书就够了

    以下是我推荐给Java开发者们的一些值得一看的好书.但是这些书里面并没有Java基础.Java教程之类的书,不是我不推荐,而是离我自己学习 Java基础技术也过去好几年了,我学习的时候看的什么也忘了, ...

  6. Keras实现简单BP神经网络

    BP 神经网络的简单实现 from keras.models import Sequential #导入模型 from keras.layers.core import Dense #导入常用层 tr ...

  7. Redis安装测试

    1.在线下载,redis是C语言开发的,编译需要依赖一个gcc的环境: yum install gcc-c++,需要保证网络畅通,在线安装: 键入y 环境安装完成后,接下来安装redis; 可以先去h ...

  8. 并发基础(八) java线程的中断机制

    文章转载自 详细分析Java中断机制 1. 引言 当我们点击某个杀毒软件的取消按钮来停止查杀病毒时,当我们在控制台敲入quit命令以结束某个后台服务时--都需要通过一个线程去取消另一个线程正在执行的任 ...

  9. 使用 IIS 过程中遇到的一些问题

    由于我最近开发的 Web 程序多是采用 Python 为主,因此大部分都是部署在 Linux 下的,自然在 Web 服务器上就选择了 Nginx,不过一些纯静态文件的 Web 应用会放在 IIS 下面 ...

  10. Requests库入门

    安装: $ pip install requests Response对象的一些基本属性: Response.status_code 请求的返回状态,正常为200 Response.text 页面的字 ...