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能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了.高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明. 方法选择的好坏 ...
随机推荐
- Lua语言中的__index,__newindex,rawget和rawset
转自:http://blog.csdn.net/wangbin_jxust/article/details/12108189 在谈及Lua中的__index,__newindex,rawget和raw ...
- javascript节点操作移出节点removeChild()
removeChild(a)是用来删除文档中的已有元素 参数a:要移出的节点 <div id="guoDiv"> <span>1</span> ...
- Hive基础之Hive的存储类型
Hive常用的存储类型有: 1.TextFile: Hive默认的存储类型:文件大占用空间大,未压缩,查询慢: 2.Sequence File:将属于以<KEY,VALUE>的形式序列化到 ...
- pig入门案例
测试数据位于:/home/hadoop/luogankun/workspace/sync_data/pigperson.txt中的数据以逗号分隔 ,zhangsan, ,lisi, ,wangwu, ...
- CSS3 圆角属性 border-radius和-webkit-border-radius使用
CSS3 圆角属性 border-radius 在 CSS3 中新增了一个 border-radius 边框半径属性,即大家常用的圆角效果.这使得制作圆角将不再麻烦,只需对所用对象加一个 border ...
- Samba 简介
SMB 代表的是服务器消息块 (Server Message Block),它是用于在 Windows 上共享文件的协议的原始名称. CIFS 代表公共 Internet 文件系统 (Common I ...
- java基础:关于java流与文件操作
1.描述:流是字节数据或字符数据序列.Java采用输入流对象和输出流对象来支持程序对数据的输入和输出.输入流对象提供了数据从源点流向程序的管道,程序可以从输入流对象读取数据:输出流对象提供了数据从程序 ...
- JavaScript中判断函数、变量是否存在
转载:http://www.jb51.net/article/67551.htm 一.是否存在指定函数 function isExitsFunction(funcName) { try { if (t ...
- linux运维工程师工作中的一些常见问题解决方法
http://blog.sina.com.cn/s/blog_b9fe247a0101anoe.html 1.shell脚本死活不执行 问题:某天研发某同事找我说帮他看看他写的shell脚本,死活不执 ...
- springboot实现国际化
1.编写配置文件 2.在application.properties中添加 i18n指的是国际化所在包名 3.实现国际化的接口 4.在配置类中