Xiangqi

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3193    Accepted Submission(s): 780

Problem Description
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”.

 
Input
The input contains no more than 40 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.
 
Output
For each test case, if the situation is checkmate, output a single word ‘YES’, otherwise output the word ‘NO’.
 
Sample Input
2 1 4
G 10 5
R 6 4

3 1 5
H 4 5
G 10 5
C 7 5

0 0 0

 
Sample Output
YES
NO

Hint


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.

 
Source
 
 
  我真的老了  。
  11年
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <map>
using namespace std;
int d[][]={{,},{-,},{,},{,-}};
struct Node{
int x;
int y;
char ch;
}node[];
char grid[][];
int n;
bool put[][];
bool cango(int x,int y){
if(x<||x>||y<||y>)
return ;
return ;
}
bool judge(int x,int y){
int i,j,xx,yy;
for(i=;i<=n;i++){
if(node[i].x==x&&node[i].y==y)
continue;
if(node[i].ch=='R'||node[i].ch=='G'){
for(j=;j<;j++){
xx=node[i].x+d[j][];
yy=node[i].y+d[j][];
while(cango(xx,yy)&&put[xx][yy]==){
if(xx==x&&yy==y)
return ;
xx+=d[j][];
yy+=d[j][];
}
}
}
else if(node[i].ch=='C'){
for(j=;j<;j++){
xx=node[i].x+d[j][];
yy=node[i].y+d[j][];
while(cango(xx,yy)&&put[xx][yy]==){
xx+=d[j][];
yy+=d[j][];
}
xx+=d[j][];
yy+=d[j][];
while(cango(xx,yy)&&put[xx][yy]==){
if(xx==x&&yy==y)
return ;
xx+=d[j][];
yy+=d[j][];
}
}
}
else if(node[i].ch=='H'){
int X=node[i].x ,h_x;
int Y=node[i].y ,h_y;
if(cango(X-,Y)&&put[X-][Y]==){
h_x=X-;
h_y=Y+;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
h_x=X-;
h_y=Y-;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
}
if(cango(X+,Y)&&put[X+][Y]==){
h_x=X+;
h_y=Y+;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
h_x=X+;
h_y=Y-;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
} if(cango(X,Y-)&&put[X][Y-]==){
h_x=X+;
h_y=Y-;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
h_x=X-;
h_y=Y-;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
} if(cango(X,Y+)&&put[X][Y+]==){
h_x=X+;
h_y=Y+;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
h_x=X-;
h_y=Y+;
if(cango(h_x,h_y)&&put[h_x][h_y]==){
if(h_x==x&&h_y==y)
return ;
}
}
}
}
return ;
}
int main(){
int ch,i,j,xx,yy,x,y,flag,sum,f;
while(scanf("%d%d%d",&n,&x,&y)){
if(n==&&x==&&y==)
break;
memset(put,,sizeof(put));
for(i=;i<=;i++)
for(j=;j<=;j++)
grid[i][j]='#';
for(i=;i<=n;i++){
cin>>node[i].ch>>node[i].x>>node[i].y;
put[node[i].x][node[i].y]=;
grid[node[i].x][node[i].y]=node[i].ch;
}
f=;
xx=x;
yy=y;
while(cango(xx,yy)){
xx+=;
if(grid[xx][yy]=='G'){
f=;
}
else if(grid[xx][yy]!='#')
break;
}
if(f){
printf("NO\n");
continue;
}
sum=flag=;
for(i=;i<;i++){
xx=x+d[i][];
yy=y+d[i][];
if(xx>=&&xx<=&&yy>=&&yy<=){
sum++;
put[xx][yy]=;
if(judge(xx,yy))
flag++;
if(grid[xx][yy]!='#')
put[xx][yy]=;
}
}
//printf("sum %d flag %d\n",sum,flag);
if(flag==sum)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

13年

#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#pragma comment(linker, "/STACK:16777216")
using namespace std ;
typedef long long LL ;
struct Point{ /*坐标点*/
int X ;
int Y ;
Point(){} ;
Point(int x,int y):X(x),Y(y){} ;
}; struct Chess{ /*棋子数据*/
string S ;
Point P ;
Chess(){} ;
Chess(string s ,Point p):S(s),P(p){} ;
}; class Man{ /*玩家*/
private :
static int d_horse[][] ;
static int d_obstacle[][] ;
int N ;
int black_x ,black_y ;
bool grid[][] ;
vector<Chess> Genaral_Or_Che_Pao ; //将 or 车 or 炮
vector<Chess> Horse ; //马
public :
Man() ;
void read(vector<Chess> ,int ,int) ;
int cango(int,int) ;
int ok_genaral_or_che_pao() ;
int ok_horse() ;
int win() ; //是否必胜
}; int Man::d_horse[][]={{-,-},{-,},{-,},{,},
{,-},{,},{-,-},{,-} } ; int Man::d_obstacle[][]={{-,},{,},{,},{,-}}; Man::Man(){ } int Man::cango(int x ,int y){
return <= x && x<= && <= y && y <= ;
} void Man::read(vector<Chess> chess,int b_x ,int b_y){
black_x = b_x ;
black_y = b_y ;
memset(grid,,sizeof(grid)) ;
Genaral_Or_Che_Pao.clear() ;
grid[black_x][black_y] = ;
string s ;
int x ,y ;
for(int i = ;i < chess.size() ;i++){
s = chess[i].S ;
x = chess[i].P.X ;
y = chess[i].P.Y ;
if(x == black_x && y == black_y)
continue ;
grid[x][y] = ;
if(s == "H")
Horse.push_back(chess[i]) ;
else
Genaral_Or_Che_Pao.push_back(chess[i]) ;
}
} int Man::ok_genaral_or_che_pao(){
int x ,y ,L ,R ,k;
string s ;
for(int i = ;i < Genaral_Or_Che_Pao.size() ;i++){
x = Genaral_Or_Che_Pao[i].P.X ;
y = Genaral_Or_Che_Pao[i].P.Y ;
s = Genaral_Or_Che_Pao[i].S ;
if(x == black_x){
L = Min(y ,black_y) + ;
R = Max(y ,black_y) - ;
int dot = ;
for(k = L ;k <= R ;k++)
dot += grid[x][k] ;
if(dot == && s == "C")
return ;
if(dot == && s!= "C")
return ;
}
if(y == black_y){
L = Min(x ,black_x) + ;
R = Max(x ,black_x) - ;
int dot = ;
for(k = L ;k <= R ;k++)
dot += grid[k][y] ;
if(dot == && s == "C")
return ;
if(dot == && s!= "C")
return ;
}
}
return ;
} int Man::ok_horse(){
int x ,y ,k ,x2 ,y2 ,x1,y1;
for(int i = ;i < Horse.size() ;i++){
x = Horse[i].P.X ;
y = Horse[i].P.Y ;
for(k = ;k < ;k++){
x2 = d_horse[k][] + x ;
y2 = d_horse[k][] + y ;
x1 = d_obstacle[k>>][] + x ;
y1 = d_obstacle[k>>][] + y ;
if(!cango(x2,y2)||!cango(x1,y1))
continue ;
if(!grid[x1][y1] && black_x == x2 && black_y == y2)
return ;
}
}
return ;
} int Man::win(){
if(ok_genaral_or_che_pao())
return ;
if(ok_horse())
return ;
return ;
} class App{
private :
vector<Chess> chess ;
bool mat[][] ;
static int d[][] ;
int N ;
int black_x ;
int black_y ;
int red_x ;
int red_y ;
public :
App(){};
App(int ,int ,int) ;
int cango(int ,int) ;
int judge() ;
void read() ;
}; App::App(int n ,int x , int y):N(n),black_x(x),black_y(y){
chess.clear() ;
memset(mat,,sizeof(mat)) ;
}; int App::d[][]={{,},{-,},{,-},{,}} ; int App::cango(int x ,int y){
return <= x && x<= && <= y && y<= ;
} void App::read(){
chess.clear() ;
string s ;
int x ,y ;
for(int i = ;i <= N ;i++){
cin>>s>>x>>y ;
mat[x][y] = ;
chess.push_back(Chess(s,Point(x,y))) ;
if(s == "G"){
red_x = x ;
red_y = y ;
}
}
} int App::judge(){
if(black_y == red_y){ //黑方 将 直接 干死 红方 将
int dot = ;
for(int i = black_x + ;i <= red_x- ;i++)
dot += mat[i][red_y] ;
if(dot == )
return ;
}
int sum = ;
for(int i = ; i < ;i++){ //黑方 将 四个方向逃跑
int x = black_x + d[i][] ;
int y = black_y + d[i][] ;
if(!cango(x,y))
continue ;
Man red ;
red.read(chess,x,y) ; //传递数据,(x,y)为此时黑方的将
if(!red.win()) //只要红方不能赢 则不是 死局
return ;
}
return ;
} int main(){
int n ,x, y;
while(cin>>n>>x>>y){
if(n==&&x==&&y==)
break ;
App app(n,x,y) ;
app.read() ;
if(app.judge())
puts("NO") ;
else
puts("YES") ;
}
return ;
}

HDU 4121 Xiangqi 我老了?的更多相关文章

  1. HDU 4121 Xiangqi 模拟题

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

  2. HDU 4121 Xiangqi (算是模拟吧)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4121 题意:中国象棋对决,黑棋只有一个将,红棋有一个帅和不定个车 马 炮冰给定位置,这时当黑棋走,问你黑 ...

  3. HDU 4121 Xiangqi --模拟

    题意: 给一个象棋局势,问黑棋是否死棋了,黑棋只有一个将,红棋可能有2~7个棋,分别可能是车,马,炮以及帅. 解法: 开始写法是对每个棋子,都处理处他能吃的地方,赋为-1,然后判断将能不能走到非-1的 ...

  4. HDU 4121 Xiangqi

    模拟吧,算是... 被这个题wa到哭,真是什么都不想说了...上代码 #include <iostream> #include <cstring> using namespac ...

  5. HDU 4121

    http://www.bnuoj.com/v3/problem_show.php?pid=10277 //#pragma comment(linker, "/STACK:16777216&q ...

  6. HDU 4461:The Power of Xiangqi(水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4461 题意:每个棋子有一个权值,给出红方的棋子情况,黑方的棋子情况,问谁能赢. 思路:注意“ if a play ...

  7. HDU 4461 The Power of Xiangqi (水题)

    题意:给定一些字母,每个字母都代表一值,如果字母中没有B,或者C,那么就在总值大于1的条件下删除1,然后比较大小. 析:没什么好说的,加起来比较就好了. 代码如下: #pragma comment(l ...

  8. *HDU 2108 计算几何

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. hdu 1010 深搜+剪枝

    深度搜索 剪枝 还不是很理解 贴上众神代码 //http://blog.csdn.net/vsooda/article/details/7884772#include<iostream> ...

随机推荐

  1. [Hibernate] - Annotations

    Hibernate使用Annotations最简单例子: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8 ...

  2. hadoop作业调优参数整理及原理(转)

    1 Map side tuning参数 1.1 MapTask运行内部原理 当map task开始运算,并产生中间数据时,其产生的中间结果并非直接就简单的写入磁盘.这中间的过程比较复杂,并且利用到了内 ...

  3. Storm的数据处理编程单元:Bolt 学习整理

    Bolt是Topology中的数据处理的单元,也是Storm针对处理过程的编程单元.Topology中所有的处理都是在这些Bolt中完成的,编程人员可以实现自定义的处理过程,例如,过滤.函数.聚集.连 ...

  4. NeHe OpenGL教程 第二十四课:扩展

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  5. MHI ,运动历史图像的的获取[下载自CSDN]

    #include "cv.h" #include "highgui.h" #include "stdlib.h" #include &quo ...

  6. A note to "On global motions of a compressible barotropic and selfgravitating gas with density-dependent viscosities"

    Ducomet, Bernard; Nečasová, Šárka; Vasseur, Alexis. On global motions of a compressible barotropic a ...

  7. Studio-----快捷键大全

    Ctrl+Alt+Space 类名或接口名提示; 补充布局的提示: 26. Ctrl+Alt+Space是类名自动完成 Ctrl+X 删除行 Ctrl+D 复制行 Alt+回车 导入包,自动修正 Cr ...

  8. SOA 与 DDD

    SOA是技术架构方面,Evans DDD则是哲学方法论方面,所属方向不一样,或者说两者非常的无关.甚至是两个不同方向.使用DDD可以将系统从无到有到大建立起来,而大到一定程度,就需要SOA,整合异构. ...

  9. xss 和 csrf攻击详解

    在那个年代,大家一般用拼接字符串的方式来构造动态 SQL 语句创建应用,于是 SQL 注入成了很流行的攻击方式.在这个年代, 参数化查询 已经成了普遍用法,我们已经离 SQL 注入很远了.但是,历史同 ...

  10. LInux SSH远程文件/目录传输命令scp(转载)

    From:http://www.vpser.net/manage/scp.html 相信各位VPSer在使用VPS是经常会在不同VPS间相互备份数据或转移数据.一般情况下VPS都已安装了Ngnix或者 ...