zoukankan      html  css  js  c++  java
  • Xiangqi(简单模拟)

    4746: Xiangqi

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte

    总提交: 15            测试通过:2

    描述

    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.

    题意:黑方只有一枚将棋,红方则有多枚棋子,此时黑方动,判断黑方是否已经无路可走,即被将死。

    题解:模拟判断红方棋子的走向,再判断黑将4个方向移动是否能被红方的棋子所将死。

      1 #include "iostream"
      2 #include "string.h"
      3 using namespace std;
      4 int weizhi[15][15];
      5 int wxl(int a,int b)
      6 {
      7     int i,flag;//flag判断此位置是否有棋子和有几个棋子 
      8 //    判断马的走位
      9  if((a-2)>0&&(b-1)>0&&weizhi[a-2][b-1]==5&&!weizhi[a-1][b-1])return 0;
     10  if((a-2)>0&&(b+1)<=9&&weizhi[a-2][b+1]==5&&!weizhi[a-1][b+1])return 0;
     11  if((a-1)>0&&(b-2)>0&&weizhi[a-1][b-2]==5&&!weizhi[a-1][b-1])return 0;
     12  if((a+1)<=10&&(b-2)>0&&weizhi[a+1][b-2]==5&&!weizhi[a+1][b-1])return 0;
     13  if((a+2)<=10&&(b-1)>0&&weizhi[a+2][b-1]==5&&!weizhi[a+1][b-1])return 0;
     14  if((a+2)<=10&&(b+1)<=9&&weizhi[a+2][b+1]==5&&!weizhi[a+1][b+1])return 0;
     15  if((a-1)>0&&(b+2)<=9&&weizhi[a-1][b+2]==5&&!weizhi[a-1][b+1])return 0;
     16  if((a+1)<=10&&(b+2)<=9&&weizhi[a+1][b+2]==5&&!weizhi[a+1][b+1])return 0;
     17  
     18 // 判断炮和车和将和帅是否直接对面 
     19 flag=0;
     20  for(i=a-1;i>0;i--)
     21  {
     22      if(!flag&&weizhi[i][b]==3||weizhi[i][b]==4)return 0;
     23      if(weizhi[i][b]==2&&flag==1)return 0;
     24      if(weizhi[i][b]!=0)flag++;//判断炮有几个跳跃点 
     25  }
     26  flag=0;
     27  for(i=a+1;i<=10;i++)
     28  {
     29      if(!flag&&weizhi[i][b]==3||weizhi[i][b]==4)return 0;
     30      if(weizhi[i][b]==2&&flag==1)return 0;
     31      if(weizhi[i][b]!=0)flag++;
     32  } 
     33  flag=0;
     34  for(i=b-1;i>0;i--)
     35  {
     36      if(!flag&&weizhi[a][i]==3||weizhi[a][i]==4)return 0;
     37      if(weizhi[a][i]==2&&flag==1)return 0;
     38      if(weizhi[a][i]!=0)flag++;
     39  }
     40  flag=0;
     41  for(i=b+1;i<10;i++)
     42  {
     43      if(!flag&&weizhi[a][i]==3||weizhi[a][i]==4)return 0;
     44      if(weizhi[a][i]==2&&flag==1)return 0;
     45      if(weizhi[a][i]!=0)flag++;
     46  }
     47  return 1;//黑将不会被将军 
     48 }
     49 
     50 int main()
     51 {
     52     int i,k,n,m,b,c,flag;
     53     char a;
     54     while(cin>>n>>m>>k)
     55     {
     56         if(n==0&&m==0&&k==0)break;
     57         memset(weizhi,0,sizeof weizhi);//给位置初始化为0 
     58         for(i=0;i<n;i++)
     59         {
     60             cin>>a>>b>>c;
     61             if(a=='G')weizhi[b][c]=4;//4表示这个点的棋子是帅  
     62             if(a=='H')weizhi[b][c]=5;//5表示这个点的棋子是马 
     63             if(a=='C')weizhi[b][c]=2;//2表示这个点的棋子是炮
     64             if(a=='R')weizhi[b][c]=3;//3表示这个点的棋子是车 
     65         }
     66         //判断将的4个移动方向是否还会被将军 
     67         if(m+1<=3&&m+1>0&&k>=4&&k<=6)//因为黑将的移动是有范围的,所以不能越界 
     68         {
     69             flag=wxl(m+1,k);
     70             if(flag)
     71             {
     72                 cout<<"NO"<<'
    ';continue;
     73             }    
     74         }
     75         if(m-1<=3&&m-1>0&&k>=4&&k<=6)
     76         {
     77             flag=wxl(m-1,k);
     78             if(flag)
     79             {
     80                 cout<<"NO"<<'
    ';continue;
     81             }
     82         }
     83         if(m<=3&&m>0&&k+1>=4&&k+1<=6)
     84         {
     85             flag=wxl(m,k+1);
     86             if(flag)
     87             {
     88                 cout<<"NO"<<'
    ';continue;
     89             }
     90         }
     91         if(m<=3&&m>0&&k-1>=4&&k-1<=6)
     92         {
     93             flag=wxl(m,k-1);
     94             if(flag)
     95             {
     96                 cout<<"NO"<<'
    ';continue;
     97             }
     98         }
     99         cout<<"YES"<<'
    ';
    100     }
    101 } 
    102 //但是TOJ有一点不用考虑,我也没考虑,其他oj则需要考虑,就是红方是用帅来将军的,这是轮到黑方
    103 //移子,那么应该是黑方胜,而TOJ则还是红方胜 
  • 相关阅读:
    小心使用宏
    常见文件、目录、路径操作函数
    链表法页框的分配和去配
    获取调试符号文件
    Visual C++ Runtime Error 调试
    HEAP: Free Heap block XXXX modified at XXXX after it was freed
    磁盘操作 API
    【转】浅谈大型网站动态应用系统架构 Mr
    jQuery1.3.1 Tab选项卡 Mr
    spring依赖注入思想 Mr
  • 原文地址:https://www.cnblogs.com/htmrc1/p/8449795.html
Copyright © 2011-2022 走看看