UVA-1589 Xiangqi (Analog)

Source: Internet
Author: User

Xiangqi
problem DescriptionXiangqi is one of the most popular Two-player board games in China. The game represents a battle between the armies with the goal of capturing the enemy's "general" piece. In this problem, you is given a situation of later stage in the game. Besides, the red side has already "delivered a check".Your work was to check whether the situation is "checkmate".

Now we introduce some basic rules of Xiangqi. Xiangqi is played on a 10x9 board and the pieces be placed on the intersections (points). The top left point is (bottom) and the "Right" is (10,9). There is groups of pieces marked by black or red Chinese characters, belonging to the players separately. During the game, each player in turn moves one piece from the point it occupies to another point. No. Pieces can occupy the same point at the same time. A piece can moved onto a point occupied by an enemy piece, in which case the enemy piece is ' captured ' and removed from The board. When the danger of being captured by the enemy player on the enemy player's next move, the enemy player is s Aid to has"delivered a check". If The general's player can make no move to prevent the "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 pointEither vertically or horizontally and cannot leave the "Palace"Unless the situation called"Flying General"(see the figure above)." Flying General "means so one general can" fly "across the board to capture the enemy general if they stand on the same L ine without intervening pieces.
Chariot:the chariots can move and capture vertically and horizontally by any distance Pieces
Cannon:the cannons move like the chariots, horizontally and vertically, but capture by jumpingexactly one piece(whether it is friendly or enemy) over-to-its target.
Horse:the Horses has 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 this direction (see the figure below), which is called "hobbling the horse ' s leg”.



Now is 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".
 
InputThe input contains no more than-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 is a char and integers representing the type and position of the piece (type char ' G ' for genera L, ' R ' for Chariot, ' H ' for Horse and ' C ' for cannon. We guarantee the situation is legal and the red side have delivered the check.
There is a blank line between the test cases. The input ends by 0 0 0.
 
OutputFor each test case, if the situation is checkmate, and output a single word ' YES ', otherwise output the word ' NO '.
 
Sample Input
2 1 4G 5R 6 1 5H 4 5G 5C 7 50 0 0
 
Sample Output
YESNOIn fact, such a problem to do a very interesting thing, to achieve a complete program of a small part of the programming ability or exercise. My idea is to The attack range  I  unexpectedly once directly, is my strength really is the test details of the water problem?? Well, my water problem is doing well. 
#include <iostream> #include <cstring> #include <string>using namespace Std;int map[13][13]; Initialize to 0,1 to indicate that there is a pawn int sign[13][13]; Initialized to 0,1 represents a struct Node{char c;int x, y;} in the range of the red pawn attack;           int main () {int n, BX, by;while (CIN >> n >> bx >> by&&n) {memset (map, 0, sizeof (map)); Initialize memset (sign, 0, sizeof), node Qizi[10];int RJX, rjy;for (int i = 0; i < n; i++) {cin >> qizi[i].c >& Gt qizi[i].x >> qizi[i].y;if (qizi[i].c = = ' G ') {rjx = Qizi[i].x;rjy = Qizi[i].y;} MAP[QIZI[I].X][QIZI[I].Y] = 1;} if (Rjy = = by)//For special cases, black will and red handsome if directly relative, Direct is no{int OK = 0;for (int i = bx + 1; i < RJX; i++) if ( MAP[I][RJY]) ok = 1;if (!ok) {cout << "NO" << Endl;continue;}} for (int i = 0; i < n; i++)//Handle the attack range of the red pawn, the pawn itself is not inside (can be eaten) {if (qizi[i].c = = ' G ')// Deal with the attack range {for (int i = rjx-1; I >= bx; i--) {Sign[i][rjy] = 1;if (Map[i][rjy]) break;}} else if (qizi[i].c = = ' R ')//deal with the attack range of the car {int xx = Qizi[i].x;int yy = qizi[i].y;for (int i = xx + 1; i <=; i++) {Sign[i][yy] = 1;if (Map[i][yy]) bre AK;} for (int i = xx-1; I >= 1; i--) {Sign[i][yy] = 1;if (Map[i][yy]) break; for (int i = yy + 1, I <= 9; i++) {Sign[xx][i] = 1;if (Map[xx][i]) break; for (int i = yy-1; I >= 1; i--) {Sign[xx][i] = 1;if [map[xx][i]) break;}  if (qizi[i].c = = ' C ')//Handle out cannon attack range {int xx = Qizi[i].x;int yy = Qizi[i].y;int Ok;ok = 0;for (int i = xx + 1; I <= 10; i++) {if (ok = = 1) sign[i][yy] = 1;if (Map[i][yy]) ok++;} OK = 0;for (int i = xx-1; I >= 1; i--) {if (ok = = 1) sign[i][yy] = 1;if (Map[i][yy]) ok++;} OK = 0;for (int i = yy + 1; I <= 9; i++) {if (ok = 1) sign[xx][i] = 1;if (map[xx][i]) ok++;} OK = 0;for (int i = yy-1; I >= 1; i--) {if (ok = = 1) sign[xx][i] = 1;if (map[xx][i]) ok++;}} if (qizi[i].c = = ' H ')//Handle the attack range {int xx = Qizi[i].x;int yy = qizi[i].y;if (!map[xx-1][yy]) {if (xx- 2 >= 1 && yy-1 >= 1) sign[xX-2][yy-1] = 1;if (xx-2 >= 1 && yy + 1 <= 9) Sign[xx-2][yy + 1] = 1;} if (!map[xx + 1][yy]) {if (xx + 2 <= && yy-1 >= 1) sign[xx + 2][yy-1] = 1;if (xx + 2 <= &&am P YY + 1 <= 9) sign[xx + 2][yy + 1] = 1;}  if (!map[xx][yy-1]) {if (yy-2 >= 1 && xx-1 >= 1) sign[xx-1][yy-2] = 1;if (yy-2 >= 1 && XX + 1 <=) Sign[xx + 1][yy-2] = 1;}  if (!map[xx][yy + 1]) {if (yy + 2 <= 9 && xx-1 >= 1) sign[xx-1][yy + 2] = 1;if (yy + 2 <= 9 && XX + 1 <=) Sign[xx + 1][yy + 2] = 1;}} /*for (int i = 1; i <=; i++)//test {for (int j = 1; J <= 9; j + +) cout << sign[i][j] << ""; cout <&lt ; Endl;}                       */int cnt = 0; Determine if the black will be four in a direction if (bx-1 >= 1) {if (Sign[bx-1][by]) cnt++;} else Cnt++;if (bx + 1 <= 3) {if (sign[bx + 1][by]) cnt++;} else Cnt++;if (by-1 >= 4) {if (sign[bx][by-1]) cnt++;} else Cnt++;if (by + 1 <= 6) {if (Sign[bx][by + 1]) cnt++;} ELSE Cnt++;//cout << CNT << endl;if (cnt = = 4) cout << "YES" << endl;else cout << "NO" &LT;&L T Endl;}}



UVA-1589 Xiangqi (Analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.