描述

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. 硬盘安装雨林木风Win7旗舰版系统教程

    硬盘安装雨林木风Win7旗舰版系统教程 安装完成,登录后报administrator无权限, F8 进入安全模式,修改administrator的权限.

  2. 1058 A+B in Hogwarts (20 分)

    1058 A+B in Hogwarts (20 分) If you are a fan of Harry Potter, you would know the world of magic has ...

  3. phpexcel导入数据出现PHPExcel_RichText Object解决办法

    在导入excel的时候会出现异常情况,有的问题出现PHPExcel_RichText object,错误代码如下 PHPExcel_RichText Object ( [_richTextElemen ...

  4. Java内部类引用外部类中的局部变量为何必须是final问题解析

    今天编写一个多线程程序,发现在方法内定义内部类时,如果内部类调用了方法中的变量,那么该变量必须申明为final类型,百思不得其解,后来想到应该是生命周期的原因,因为方法内定义的变量是局部变量,离开该方 ...

  5. tornado系列文章

    http://www.nowamagic.net/academy/detail/13321030

  6. 两个栈实现队列 Python实现

    # coding=utf-8 MAX_LENGTH = 100 SUCCESS = 1 FAIL = 0 ERROR = -1 class Queue(object): stack_fir = Non ...

  7. msq_table's methods

    -- 查看有哪些用户 host: % 代表任意地址都可以登录 host: localhost 代表仅本地可以连接select host,user from mysql.user; -- 建库 crea ...

  8. Flutter main future mirotask 的执行顺序

    下面这段代码的输出是什么? import 'dart:async'; main() { print('main #1 of 2'); scheduleMicrotask(() => print( ...

  9. [Flutter] Image.File 加载图像时文件内容变化显示不变解决

    在Flutter中,我们可以用下面的代码从文件中加载图像: Image.file(File(_fileName)); 这个时候,当_fileName这个文件名称和路径不变,文件内容变化时,Flutte ...

  10. Windows系统日常运维

    WINDOWS系统日常运维 http://www.docin.com/p-677263438.html