HDU 4121 Xiangqi 我老了?

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[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct Node{
    int x;
    int y;
    char ch;
}node[100];
char grid[12][12];
int n;
bool put[12][12];
bool cango(int x,int y){
   if(x<1||x>10||y<1||y>9)
     return 0;
   return 1;
}
bool judge(int x,int y){
    int i,j,xx,yy;
    for(i=1;i<=n;i++){
        if(node[i].x==x&&node[i].y==y)
            continue;
       if(node[i].ch=='R'||node[i].ch=='G'){
           for(j=0;j<4;j++){
               xx=node[i].x+d[j][0];
               yy=node[i].y+d[j][1];
             while(cango(xx,yy)&&put[xx][yy]==0){
               if(xx==x&&yy==y)
                  return 1;
               xx+=d[j][0];
               yy+=d[j][1];
             }
           }
       }
       else if(node[i].ch=='C'){
           for(j=0;j<4;j++){
               xx=node[i].x+d[j][0];
               yy=node[i].y+d[j][1];
             while(cango(xx,yy)&&put[xx][yy]==0){
               xx+=d[j][0];
               yy+=d[j][1];
             }
               xx+=d[j][0];
               yy+=d[j][1];
             while(cango(xx,yy)&&put[xx][yy]==0){
               if(xx==x&&yy==y)
                  return 1;
               xx+=d[j][0];
               yy+=d[j][1];
             }
           }
       }
       else if(node[i].ch=='H'){
           int X=node[i].x ,h_x;
           int Y=node[i].y ,h_y;
           if(cango(X-1,Y)&&put[X-1][Y]==0){
              h_x=X-2;
              h_y=Y+1;
             if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                    return 1;
             }
              h_x=X-2;
              h_y=Y-1;
             if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                  return 1;
             }
           }
           if(cango(X+1,Y)&&put[X+1][Y]==0){
              h_x=X+2;
              h_y=Y+1;
              if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                  return 1;
              }
              h_x=X+2;
              h_y=Y-1;
              if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                  return 1;
              }
           }

           if(cango(X,Y-1)&&put[X][Y-1]==0){
              h_x=X+1;
              h_y=Y-2;
             if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                  return 1;
              }
              h_x=X-1;
              h_y=Y-2;
             if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                  return 1;
             }
           }

            if(cango(X,Y+1)&&put[X][Y+1]==0){
              h_x=X+1;
              h_y=Y+2;
              if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                  return 1;
             }
              h_x=X-1;
              h_y=Y+2;
              if(cango(h_x,h_y)&&put[h_x][h_y]==0){
                 if(h_x==x&&h_y==y)
                  return 1;
             }
           }
       }
     }
   return 0;
}
int main(){
   int ch,i,j,xx,yy,x,y,flag,sum,f;
   while(scanf("%d%d%d",&n,&x,&y)){
       if(n==0&&x==0&&y==0)
          break;
        memset(put,0,sizeof(put));
        for(i=1;i<=10;i++)
          for(j=1;j<=9;j++)
             grid[i][j]='#';
       for(i=1;i<=n;i++){
          cin>>node[i].ch>>node[i].x>>node[i].y;
          put[node[i].x][node[i].y]=1;
          grid[node[i].x][node[i].y]=node[i].ch;
       }
       f=0;
       xx=x;
       yy=y;
       while(cango(xx,yy)){
            xx+=1;
          if(grid[xx][yy]=='G'){
               f=1;
          }
          else  if(grid[xx][yy]!='#')
            break;
       }
       if(f){
          printf("NO\n");
          continue;
       }
       sum=flag=0;
      for(i=0;i<4;i++){
         xx=x+d[i][0];
         yy=y+d[i][1];
         if(xx>=1&&xx<=3&&yy>=4&&yy<=6){
           sum++;
           put[xx][yy]=0;
           if(judge(xx,yy))
              flag++;
           if(grid[xx][yy]!='#')
            put[xx][yy]=1;
         }
      }
      //printf("sum %d   flag %d\n",sum,flag);
     if(flag==sum)
       printf("YES\n");
     else
       printf("NO\n");
   }
  return 0;
}

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[8][2] ;
       static int d_obstacle[4][2] ;
       int N ;
       int black_x ,black_y ;
       bool grid[12][12] ;
       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[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},
                        {2,-1},{2,1},{-1,-2},{1,-2} } ;

int Man::d_obstacle[4][2]={{-1,0},{0,1},{1,0},{0,-1}};

Man::Man(){

}

int Man::cango(int x ,int y){
    return 1 <= x && x<= 10 && 1 <= y && y <= 9 ;
}

void Man::read(vector<Chess> chess,int b_x ,int b_y){
    black_x = b_x ;
    black_y = b_y ;
    memset(grid,0,sizeof(grid)) ;
    Genaral_Or_Che_Pao.clear() ;
    grid[black_x][black_y] = 1 ;
    string s ;
    int x ,y ;
    for(int i = 0 ;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] = 1 ;
        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 = 0 ;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) + 1 ;
          R = Max(y ,black_y) - 1 ;
          int dot = 0 ;
          for(k = L ;k <= R ;k++)
              dot += grid[x][k] ;
          if(dot == 1 && s == "C")
             return 1 ;
          if(dot == 0 && s!= "C")
             return 1 ;
       }
       if(y == black_y){
          L = Min(x ,black_x) + 1 ;
          R = Max(x ,black_x) - 1 ;
          int dot = 0 ;
          for(k = L ;k <= R ;k++)
              dot += grid[k][y] ;
          if(dot == 1 && s == "C")
             return 1 ;
          if(dot == 0 && s!= "C")
             return 1 ;
       }
   }
   return 0 ;
}

int Man::ok_horse(){
    int x ,y ,k ,x2 ,y2 ,x1,y1;
    for(int i = 0 ;i < Horse.size() ;i++){
       x = Horse[i].P.X ;
       y = Horse[i].P.Y ;
       for(k = 0 ;k < 8 ;k++){
           x2 = d_horse[k][0] + x ;
           y2 = d_horse[k][1] + y ;
           x1 = d_obstacle[k>>1][0] + x ;
           y1 = d_obstacle[k>>1][1] + y ;
           if(!cango(x2,y2)||!cango(x1,y1))
               continue  ;
           if(!grid[x1][y1] && black_x == x2 && black_y == y2)
              return 1 ;
       }
    }
    return 0 ;
}

int Man::win(){
    if(ok_genaral_or_che_pao())
       return 1 ;
    if(ok_horse())
       return 1 ;
    return 0 ;
}

class App{
  private :
     vector<Chess> chess ;
     bool mat[12][12] ;
     static int d[4][2] ;
     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,0,sizeof(mat)) ;
};

int App::d[4][2]={{1,0},{-1,0},{0,-1},{0,1}} ;

int App::cango(int x ,int y){
    return 1 <= x && x<= 3 && 4 <= y && y<= 6 ;
}

void App::read(){
    chess.clear() ;
    string s ;
    int x ,y ;
    for(int i = 1 ;i <= N ;i++){
        cin>>s>>x>>y ;
        mat[x][y] = 1 ;
        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 = 0;
        for(int i = black_x + 1 ;i <= red_x-1 ;i++)
            dot += mat[i][red_y] ;
        if(dot == 0)
           return 1 ;
     }
     int sum = 0 ;
     for(int i = 0 ; i < 4 ;i++){   //黑方 将 四个方向逃跑
        int x = black_x + d[i][0] ;
        int y = black_y + d[i][1] ;
        if(!cango(x,y))
           continue ;
        Man red ;
        red.read(chess,x,y) ;   //传递数据,(x,y)为此时黑方的将
        if(!red.win())        //只要红方不能赢 则不是 死局
           return 1 ;
     }
     return 0 ;
}

int main(){
   int n ,x, y;
   while(cin>>n>>x>>y){
       if(n==0&&x==0&&y==0)
          break ;
       App app(n,x,y) ;
       app.read() ;
       if(app.judge())
          puts("NO") ;
       else
          puts("YES") ;
   }
   return 0  ;
}

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3452782.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值