TZOJ 4746 Xiangqi(模拟棋盘数组)
描述
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(模拟棋盘数组)的更多相关文章
- TZOJ 4813 机器翻译(模拟数组头和尾)
描述 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先在内存中查找这 ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
- Clumsy Keke【模拟+三维数组】
Clumsy Keke 题目链接(点击) Problem Description Keke is currently studying engineering drawing courses, and ...
- JS模拟实现数组的map方法
昨天使用map方法的时候,突然感觉一直在直接用,也没有试试是怎么实现的,本来想直接搜一篇文章盘一下子,结果没搜到合适的,好吧,那就自己来写一下子吧 今天就来实现一个简单的map方法 首先我们来看一下m ...
- 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 ...
- TZOJ 5280 搜索引擎(模拟字符串)
描述 谷歌.百度等搜索引擎已经成为了互连网中不可或缺的一部分.在本题中,你的任务也是设计一个搜索论文的搜索引擎,当然,本题的要求比起实际的需求要少了许多. 本题的输入将首先给出一系列的论文,对于每篇论 ...
- ACM-ICPC北京赛区(2017)网络赛1【模拟+枚举+数组操作】
题目1 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n ...
- hdu4121 poj4001 Xiangqi(模拟)
模拟题考验coding能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了.高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明. 方法选择的好坏 ...
随机推荐
- nsenter工具进入docker容器
对于运行在后台的Docker容器,我们经常需要做的事情是进入到容器中,docker为我们提供了docker exec .docker attach 命令,并且还提供了nsenter工具,外部工具供我们 ...
- 1035 Password (20 分)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for th ...
- Maven 配置tomcat插件
使用tomcat插件来访问maven 1 先下载tomcat插件(在pom中配置) <!-- 配置Tomcat插件 --> <plugin> <groupId>or ...
- Eclipse变量名自动补全问题 自定义上屏按键为TAB
Eclipse空格等号等都可以上屏,这样有时候输入变量名再按空格就会自动补全,非常讨厌.那么怎么办呢? 1.首先你的Eclipse需要装有 Eclipse plug-in development en ...
- Js 动态设置DIV日期信息
HTML代码如下: <div id="time"> 2013年12月20日 14:49:02 星期五 </div> JS代码如下: window.onlo ...
- JQ-用户注册用到的图形验证码,短信验证码点击事件,切换active类
// 点击切换图形验证码 页面加载完后执行,类似window.onload $(function () { var imgCaptcha = $(".img-captcha"); ...
- hammer使用: 代码:捏合、捏开、图片放大 的一个手机图片“放大缩小可拖动”的小效果
hammer.js 的使用. (手机手势插件) 捏合.捏开.图片放大 的一个手机图片“放大缩小可拖动”的小效果. 相关js 到 http://www.bootcdn.cn/ 查找和下载. hamme ...
- (18/24) webpack实战技巧:快速入门webpack模块化配置
搞个小例子便于学习: 具体操作为把上节中的webpack.config.js中的entry入口文件进行模块化设置,单独拿出来制作成一个模块. 1.在根目录新建一个config文件,然后新建webpac ...
- 网络软工个人作业4——Alpha阶段个人总结
1.个人总结 (1) 类型 具体技能和面试问题 现在的回答 毕业时找工作 语言 拿手的语言 Java 软件实现 有没有在别人的代码基础上进行改进,你是怎么读懂别人的代码,你采取什么方法不影响原来的功能 ...
- 深入浅出理解依赖注入这种由外部负责其依赖需求的行为,我们可以称其为 “控制反转(IoC)”
原文地址: http://www.insp.top/learn-laravel-container ,转载务必保留来源,谢谢了! 这个组件现在可以很简单的获取到它所需要的服务,服务采用延迟加载的方式, ...