poj 4001 Xiangqi(模拟)

Xiangqi

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1357   Accepted: 347

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 right figure). “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 left figure), which is called “hobbling the horse’s leg”.

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.

Situation 1

Situation 2

Source

Fuzhou 2011

题意:就是判断是不是将死了。

分析:以将出发,因为一开始肯定是将军的,所以只要判断将走一步后是不是安全的就行了。

Ps:一次比赛中碰到这题,交给队友写的,写完wa,又找不到问题,我也没仔细看他代码,今天仔细看了看,就一点小问题啊!!就稍微做了些修改啊!!,就这么ac了啊!!T_T

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int vis[15][15];
bool search(int a,int b)
{
    int i,j,t;
    ///这是判断马的- -
    if(a-2>0&&b-1>0&&vis[a-2][b-1]==5&&!vis[a-1][b-1]) return 0;
    if(b-2>0&&a-1>0&&vis[a-1][b-2]==5&&!vis[a-1][b-1]) return 0;
    if(a-2>0&&b+1<=9&&vis[a-2][b+1]==5&&!vis[a-1][b+1]) return 0;
    if(b+2<=9&&a-1>0&&vis[a-1][b+2]==5&&!vis[a-1][b+1]) return 0;
    if(a+2<=10&&b+1<=9&&vis[a+2][b+1]==5&&!vis[a+1][b+1]) return 0;
    if(b-2>0&&a+1<=10&&vis[a+1][b-2]==5&&!vis[a+1][b-1]) return 0;
    if(a+2<=10&&b-1>0&&vis[a+2][b-1]==5&&!vis[a+1][b-1]) return 0;
    if(b+2<=9&&a+1<=10&&vis[a+1][b+2]==5&&!vis[a+1][b+1]) return 0;

    ///然后是炮和车,t记录有几个隔子
    t=0;
    for(i=a-1; i>=1; i--)
    {
        if(!t&&(vis[i][b]==3||vis[i][b]==2)) return 0;///车

        if(vis[i][b]==4&&t==1)return 0;///炮
        if(vis[i][b]!=0) t++;
    }
    t=0;
    for(i=a+1; i<=10; i++)
    {
        if(!t&&(vis[i][b]==3||vis[i][b]==2)) return 0;

        if(vis[i][b]==4&&t==1)return 0;
        if(vis[i][b]!=0) t++;
    }
    t=0;
    for(i=b-1; i>=1; i--)
    {
        if(!t&&(vis[a][i]==3||vis[a][i]==2)) return 0;

        if(vis[a][i]==4&&t==1)return 0;
        if(vis[a][i]!=0) t++;
    }
    t=0;
    for(i=b+1; i<=9; i++)
    {
        if(!t&&(vis[a][i]==3||vis[a][i]==2)) return 0;

        if(vis[a][i]==4&&t==1)return 0;
        if(vis[a][i]!=0) t++;
    }
    return 1;
}
int main()
{
    int n,xx,yy,i,j,aa,bb,x,y;
    char c;
    while(~scanf("%d%d%d",&n,&xx,&yy))
    {
        aa=0,bb=0;
        if(n==0&&xx==0&&yy==0)
            break;
        memset(vis,0,sizeof(vis));
        for(i=1; i<=n; i++)
        {
          //  getchar();
         //   scanf("%c %d %d",&c,&x,&y);
            cin>>c>>x>>y;///scanf输入会出错
            if(c=='G')
            {
                vis[x][y]=2;
                aa=x,bb=y;
            }
            else if(c=='R')
                vis[x][y]=3;
            else if(c=='C')
                vis[x][y]=4;
            else if(c=='H')
                vis[x][y]=5;
        }
        int k;
        if(xx+1<=3&&xx+1>=1)///将不能出界
        {
            k=search(xx+1,yy);
            if(k)
            {
                printf("NO\n");
                continue;
            }
        }
        if(xx-1>=1&&xx-1<=3)
        {
            k=search(xx-1,yy);
            if(k)
            {
                printf("NO\n");
                continue;
            }
        }
        if(yy+1<=6&&yy+1>=4)
        {
            k=search(xx,yy+1);
            if(k)
            {
                printf("NO\n");
                continue;
            }
        }
        if(yy-1>=4&&yy-1<=6)
        {
            k=search(xx,yy-1);
            if(k)
            {
                printf("NO\n");
                continue;
            }
        }
        printf("YES\n");
    }
    return 0;
}
时间: 2024-10-19 07:45:57

poj 4001 Xiangqi(模拟)的相关文章

[ACM] POJ 1068 Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

【POJ 1028】模拟浏览器

本题要求模拟浏览器的前进.后退等操作. 用两个栈实现,一个控制前进,一个控制后退. 在前进与后退操作中,从一个栈中弹出栈顶元素,压入另一个栈中. 当打开一个新网页时,将前进栈清空. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include

POJ 1835 大模拟

宇航员 #include<iostream> #include<cstdio> #include<string> #include<cstring> #define maxn 10010 using namespace std; int a[7],temp[7]; char str[10]; void solve(int str2[],int str3[]) { if(strcmp(str,"forward")==0)//方向不变 { s

POJ 1102 LC-Display 模拟

Description A friend of you has just bought a new computer. Until now, the most powerful computer he ever used has been a pocket calculator. Now, looking at his new computer, he is a bit disappointed, because he liked the LC-display of his calculator

HDU 4121 Xiangqi 模拟

原题: http://acm.hdu.edu.cn/showproblem.php? pid=4121 题目: Xiangqi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4809 Accepted Submission(s): 1134 Problem Description Xiangqi is one of the most pop

poj 2160 Box 模拟

题意: 给6块木板的长度,判断它们是否可以组成一个长方体. 分析: 模拟,首先判断6块木板是否可以分为3组完全相等的木板,再判断这3组木板是否能组成长方体. 代码: //poj 2160 //sep9 #include <iostream> #include <algorithm> using namespace std; int vis[8]; int x[8],y[8]; int a[8]; int main() { int i,j; for(i=0;i<6;++i){

poj 1021 2D-Nim 模拟

题意: 给两个平面,每个平面上有一些点,相邻的点可构成点集,为两个平面内的点集是够都对应相似.两个点集相似是指经过对称或旋转或平移后相等. 分析: 直接模拟判断. 代码: //poj 1021 //sep9 #include <iostream> #include <vector> #include <algorithm> using namespace std; int w,h,n; int g[128][128]; int vis[128][128]; int di

UVa 1589 Xiangqi(模拟 HDU4121)

题意  给你一个黑方被将军的象棋残局  判断红方是否已经把黑方将死 模拟题  注意细节就行了  看黑方的将是否四个方向都不能走 #include<cstdio> #include<cstring> using namespace std; const int N = 12; char brd[N][N]; int dx[] = { -1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; int hx[] = { -2, -1, -2, -1, 1, 2, 1, 2}

hdu4121 poj4001 Xiangqi(模拟)

模拟题考验coding能力,一定要思路清晰,按照模块化思想,有哪些情况,需要哪些功能都要事先分析好了.高手的模拟题代码往往结构很清晰,功能模块写成函数,没有过多重复代码,让人一看便明. 方法选择的好坏会影响编程复杂度,这题老将最多只能往四个位置走,就枚举这四个位置,每个位置再枚举每个红子看是不是有子能吃了它.枚举时注意有可能老将这一步吃了一个红子. 有一种情况是一开始双方老将就见面,这其实不叫红棋在将军,实际中红棋不能这么将.但是毕竟是一道编程题,出题人可能也不是太懂棋...所以要考虑这种情况.