日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 4121 Xiangqi

發布時間:2024/4/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 4121 Xiangqi 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

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.

AC

  • 細心模擬題
  • 先把紅方的棋讀進來,然后每個棋子的殺傷范圍,最后判斷黑將能否移動
  • 如果一直WA就好好debug去吧。。。
#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> #include <map> #include <bitset> #include <set> #include <string.h> #include <cmath> #include <queue> #include <algorithm> #define N 100005 #define P pair<int,int> #define ll long long #define lowbit(a) a&(-a) #define mk(a, b) make_pair(a, b) #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; char a[100][100]; int vis[100][100]; // 將紅帥的殺傷標記 void cG(int x, int y) {for (int i = x - 1; i >= 1; --i) {vis[i][y] = 1;if (a[i][y] != '0') break;}return; }// 將車的殺傷標記 void cR(int x, int y) {// 右 for (int i = y + 1; i <= 9; ++i) {vis[x][i] = 1;if (a[x][i] != '0') break;}// 下 for (int i = x + 1; i <= 10; ++i) {vis[i][y] = 1;if (a[i][y] != '0') break;}// 左 for (int i = y - 1; i >= 1; --i) {vis[x][i] = 1;if (a[x][i] != '0') break;}// 上 for (int i = x - 1; i >= 1; --i) {vis[i][y] = 1;if (a[i][y] != '0') break;}return; } // 判斷馬的殺傷是否合法 bool judge(int x, int y) {if (x >= 1 && x <= 10 && y >= 1 && y <= 9)return true;elsereturn false; } void cH(int x, int y) {int xx , yy;// 1xx = x - 2;yy = y - 1;if (judge(xx, yy) && judge(x - 1, y) && a[x - 1][y] == '0') {vis[xx][yy] = 1;}yy = y + 1; if (judge(xx, yy) && judge(x - 1, y) && a[x - 1][y] == '0') {vis[xx][yy] = 1;}// 2xx = x - 1;yy = y - 2;if (judge(xx, yy) && judge(x , y - 1) && a[x][y - 1] == '0') {vis[xx][yy] = 1;}yy = y + 2;if (judge(xx, yy) && judge(x , y + 1) && a[x][y + 1] == '0') {vis[xx][yy] = 1;}// 3xx = x + 1;yy = y - 2;if (judge(xx, yy) && judge(x , y - 1) && a[x][y - 1] == '0') {vis[xx][yy] = 1;}yy = y + 2;if (judge(xx, yy) && judge(x , y + 1) && a[x][y + 1] == '0') {vis[xx][yy] = 1;}// 4xx = x + 2;yy = y - 1;if (judge(xx, yy) && judge(x + 1 , y) && a[x + 1][y] == '0') {vis[xx][yy] = 1;}yy = y + 1;if (judge(xx, yy) && judge(x + 1 , y ) && a[x + 1][y] == '0') {vis[xx][yy] = 1;} } // 將炮的殺傷標記 void cC(int x, int y) {// 右 for (int i = y + 1; i <= 9; ++i) {if (a[x][i] != '0') {for (int j = i + 1; j <= 9; ++j) {vis[x][j] = 1;if (a[x][j] != '0')break;}break;}}// 下 for (int i = x + 1; i <= 10; ++i) {if (a[i][y] != '0') {for (int j = i + 1; j <= 10; ++j) {vis[j][y] = 1;if (a[j][y] != '0')break;}break;}}// 左 for (int i = y - 1; i >= 1; --i) {if (a[x][i] != '0') {for (int j = i - 1; j >= 1; --j) {vis[x][j] = 1;if (a[x][j] != '0')break;}break;}}// 上 for (int i = x - 1; i >= 1; --i) {if (a[i][y] != '0') {for (int j = i - 1; j >= 1; --j) {vis[j][y] = 1;if (a[j][y] != '0')break;}break;}}return; } // 判斷黑將的位置是否合法 bool judgeG(int x, int y) {if (x >= 1 && x <= 3 && y <= 6 && y >= 4)return true;elsereturn false; } // 判斷黑將4個方向時候可以走 bool judge_ans(int x, int y) {if (judgeG(x + 1, y) && vis[x + 1][y] == 0)return true; if (judgeG(x - 1, y) && vis[x - 1][y] == 0)return true;if (judgeG(x , y + 1) && vis[x][y + 1] == 0)return true;if (judgeG(x , y - 1) && vis[x][y - 1] == 0)return true;return false; }int main(){ #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifint n, xx, yy;while (cin >> n >> xx >> yy, n + xx + yy) {char c;int x, y;// 0 表示沒有棋子,可以走 mem(a, '0');// 0 表示可以走 mem(vis, 0);for (int i = 0; i < n; ++i) {cin >> c >> x >> y;a[x][y] = c;}for (int i = 10; i >= 1; --i) {for (int j = 1; j <= 9; ++j) {if (a[i][j] == 'C') {cC(i, j);}if (a[i][j] == 'R') {cR(i, j);}if (a[i][j] == 'G') {cG(i, j);}if (a[i][j] == 'H') {cH(i, j);}}}if (judge_ans(xx, yy))cout << "NO\n";else cout << "YES\n";} #ifndef ONLINE_JUDGEfclose(stdin); #endif return 0; }

總結

以上是生活随笔為你收集整理的HDU 4121 Xiangqi的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。