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’.
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 on the right.
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
题目类型:简单模拟
算法分析:对于每个黑色general可以移动到的位置,查找是否有红色棋子可以攻击到。对于车,炮,马和国王来说,从待查找位置开始向四周查找比较好写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
/************************************************** filename :k.cpp author :maksyuki created time :2017/12/7 22:57:55 last modified :2017/12/7 23:15:31 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; char v[12][12]; int dx1[4] = {1, -1, 0, 0}; int dy1[4] = {0, 0, 1, -1}; int dx2[8] = {-2, -1, -2, -1, 1, 2, 1, 2}; int dy2[8] = {-1, -2, 1, 2, -2, -1, 2, 1}; int dx3[8] = {-1, -1, -1, -1, 1, 1, 1, 1}; int dy3[8] = {-1, -1, 1, 1, -1, -1, 1, 1}; int n, bx, by; bool check(int xx, int yy) { if(xx <= 0 || xx >= 4 || yy <= 3 || yy >= 7) return 1; for(int i = 0; i < 4; i++) { int tmpx = xx + dx1[i], tmpy = yy + dy1[i]; int cnt = 0; for(; tmpx >= 1 && tmpx <= 10 && tmpy >= 1 && tmpy <= 9; tmpx += dx1[i], tmpy += dy1[i]) { if(v[tmpx][tmpy]) { if((v[tmpx][tmpy] == 'R' || v[tmpx][tmpy] == 'G') && !cnt) return 1; if(v[tmpx][tmpy] == 'C' && cnt == 1) return 1; cnt++; } } } for(int i = 0; i < 8; i++) { int tmpx = xx + dx2[i], tmpy = yy + dy2[i]; if(tmpx >= 1 && tmpy <= 10 && tmpy >= 1 && tmpy <= 9) { int bbx = xx + dx3[i], bby = yy + dy3[i]; if(v[tmpx][tmpy] == 'H' && !v[bbx][bby]) return 1; } } return 0; } int main() { #ifdef LOCAL CFF; //CFO; #endif while(scanf("%d %d %d", &n, &bx, &by) != EOF) { if(!n && !bx && !by) break; char c; int xx, yy; memset(v, 0, sizeof(v)); for(int i = 1; i <= n; i++) { scanf(" %c %d %d", &c, &xx, &yy); v[xx][yy] = c; } int ans = 0; for(int i = 0; i < 4; i++) { int tmpx = bx + dx1[i], tmpy = by + dy1[i]; ans += check(tmpx, tmpy); } if(ans >= 4) puts("YES"); else puts("NO"); } return 0; } |