码迷,mamicode.com
首页 > 其他好文 > 详细

poj 4001 Xiangqi(模拟)

时间:2016-05-22 12:23:02      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

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

 
题意:就是判断是不是将死了。
分析:以将出发,因为一开始肯定是将军的,所以只要判断将走一步后是不是安全的就行了。
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;
}

poj 4001 Xiangqi(模拟)

标签:

原文地址:http://blog.csdn.net/d_x_d/article/details/51469825

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!