1. 程式人生 > >Xiangqi(比較復雜的模擬)

Xiangqi(比較復雜的模擬)

範圍 ike size details 分享 post checked xiang images

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"<<\n;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"<<\n;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"<<\n;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"<<\n;continue;
 97             }
 98         }
 99         cout<<"YES"<<\n;
100     }
101 } 
102 //但是TOJ有一點不用考慮,我也沒考慮,其他oj則需要考慮,就是紅方是用帥來將軍的,這是輪到黑方
103 //移子,那麽應該是黑方勝,而TOJ則還是紅方勝 

Xiangqi(比較復雜的模擬)