Xiangqi, ACM/ICPC Fuzhou 2011, UVa1589

Xiangqi, ACM/ICPC Fuzhou 2011, UVa1589

第一篇博文,来记录一下自己刷算法紫书的进程

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 sleg”.

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’.
 在这里插入图片描述
在这里插入图片描述
做法很简单,暴力枚举即可,要注意几个点

  • 第一次的时候将和帅可能会直接打照面,这个时候是不会被将死的,输出"NO"
  • 在模拟的时候要知道将是不能当炮架的
  • 将是可以吃子的,如果可以吃子要吃完子再判断一次
  • 在确定车能吃到的范围时不要把自己的帅也给算进去了(不过应该只有我这么弱智
  • 注意别马脚的情况和移动后出棋盘的情况,要筛选掉

我先讲下我的代码的思路,代码写的应该是相当有问题的以后还要修改,我是先把所有的子都读进来,判断是否将帅对面,然后在虚拟棋盘上标记所有的红方子能到的点,之后移动黑将看看能否通过移动或者吃子来避免被将死的情形,代码如下

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <string>
#include <cmath>
#include <cctype>
#include <stack>
using namespace std;
char board[10][9];
bool ischess(int i, int j) {
	if (board[i][j] == 'C' || board[i][j] == 'H' || board[i][j] == 'R' || board[i][j] == 'B')
		return true;
	return false;
}
bool ischess1(int i, int j) {
	if (board[i][j] == 'C' || board[i][j] == 'H' || board[i][j] == 'R'||board[i][j]=='G')
		return true;
	return false;
}
void solver(int i, int j) {
	int Ri, Rj;
	bool flag1 = true;
	if (board[i][j] == 'R') {
		Ri = i;
		Rj = j;
		for (int i = Ri - 1; i >= 0 && !ischess1(i, Rj); i--)
			if(board[i][Rj]!='B')
			board[i][Rj] = 'X';
		for (int i = Ri + 1; i < 10 && !ischess1(i, Rj); i++)
			if (board[i][Rj] != 'B')
			board[i][Rj] = 'X';
		for (int j = Rj - 1; j >= 0 && !ischess1(Ri, j); j--)
			if (board[Ri][j] != 'B')
			board[Ri][j] = 'X';
		for (int j = Rj + 1; j < 9 && !ischess1(Ri, j); j++)
			if (board[Ri][j] != 'B')
			board[Ri][j] = 'X';
	}
}
void solveg(int i, int j) {
	int Gi, Gj;
	bool flag1 = true;
	int k;
	if (board[i][j] == 'G') {
		Gi = i;
		Gj = j;
		for (int i = Gi - 1; i >= 0 && !ischess1(i, Gj); i--)
			if (board[i][Gj] != 'B')
				board[i][Gj] = 'X';

	}
}
int getpaoupper(int i, int j) {
	for (int k = i - 1; k >= 0; k--) {
		if (ischess1(k, j))
			return k;
	}
	return -1;
}
int getpaodown(int i, int j) {
	for (int k = i + 1; k <10; k++) {
		if (ischess1(k, j))
			return k;
	}
	return -1;
}
int getpaoleft(int i, int j) {
	for (int k = j - 1; k >= 0; k--) {
		if (ischess1(i, k))
			return k;
	}
	return -1;
}
int getpaoright(int i, int j) {
	for (int k = j + 1; k < 9; k++) {
		if (ischess1(i, k))
			return k;
	}
	return -1;
}
void solvec(int i, int j) {
	int Ci, Cj;

	if (board[i][j] == 'C') {
		Ci = i;
		Cj = j;
		int up = getpaoupper(Ci, Cj);
		if (up != -1) {
			for (int t = up - 1; t >= 0 && !ischess1(t, Cj); t--)
				if(board[t][Cj]!='B')
				board[t][Cj] = 'X';
		}
		int down = getpaodown(Ci, Cj);
		if (down != -1) {
			for (int t = down + 1; t < 10 && !ischess1(t, Cj); t++)
				if(board[t][Cj]!='B')
				board[t][Cj] = 'X';
		}
		int left = getpaoleft(Ci, Cj);
		if (left != -1) {
			for (int t = left - 1; t >= 0 && !ischess1(Ci, t); t--)
				if(board[Ci][t]!='B')
				board[Ci][t] = 'X';
		}
		int right = getpaoright(Ci, Cj);
		if (right != -1) {
			for (int t = right + 1; t < 9 && !ischess1(Ci, t); t++)
				if (board[Ci][t] != 'B')
				board[Ci][t] = 'X';
		}
	}


}
void solveh(int i, int j) {
	if (j + 2 < 9 && i - 1 >= 0 && !ischess(i, j + 1) && !ischess(i - 1, j + 2))
		board[i - 1][j + 2] = 'X';
	if (j + 2 < 9 && i + 1 < 9 && !ischess(i, j + 1) && !ischess(i + 1, j + 2))
		board[i + 1][j + 2] = 'X';
	if (j - 2 >= 0 && i + 1 < 9 && !ischess(i, j - 1) && !ischess(i + 1, j - 2))
		board[i + 1][j - 2] = 'X';
	if (j - 2 >= 0 && i - 1 >= 0 && !ischess(i, j - 1) && !ischess(i - 1, j - 2))
		board[i - 1][j - 2] = 'X';
	if (i - 2 >= 0 && j - 1 >= 0 && !ischess(i - 1, j) && !ischess(i - 2, j - 1))
		board[i - 2][j - 1] = 'X';
	if (i - 2 >= 0 && j + 1 <9 && !ischess(i - 1, j) && !ischess(i - 2, j + 1))
		board[i - 2][j + 1] = 'X';
	if (i + 2 < 10 && j + 1 <9 && !ischess(i + 1, j) && !ischess(i + 2, j + 1))
		board[i + 2][j + 1] = 'X';
	if (i + 2 < 10 && j - 1 >= 0 && !ischess(i + 1, j) && !ischess(i + 2, j - 1))
		board[i + 2][j - 1] = 'X';

}
bool caneat(int bgx, int bgy) {
	bool flag = false;
	for (int i = bgx; i < 10; i++) {
		if (board[i][bgy - 1] == 'G') {
			flag = true;
			break;
		}
		else if (board[i][bgy - 1] == 'C' || board[i][bgy - 1] == 'H' || board[i][bgy - 1] == 'R')
			break;
	}
	return flag;
}
void reset() {
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 9; j++)
			if(board[i][j]=='X')
			board[i][j] = '0';
	}
}
void push() {
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 9; j++) {
			if (board[i][j] == 'G')
				solveg(i, j);
			else if (board[i][j] == 'R')
				solver(i, j);
			else if (board[i][j] == 'C')
				solvec(i, j);
			else if (board[i][j] == 'H')
				solveh(i, j);
		}
	}
}
int main()
{
	//freopen("Text.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int bgx, bgy;
	int chessnum;
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 9; j++)
			board[i][j] = '0';
	}
	while (cin>>chessnum>>bgx>>bgy && chessnum) {
		//getchar();
		bool flag = false;
		for (int i = 0; i < chessnum; i++) {
			char c; int a; int b;
			cin >> c >> a >> b;
			//scanf("%c %d %d", &c, &a, &b);
			board[a - 1][b - 1] = c;
			//getchar();
		}
		board[bgx - 1][bgy - 1] = 'B';
		if (caneat(bgx, bgy)) {
			printf("NO\n");
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 9; j++)
					board[i][j] = '0';
			}
			goto A;
		}
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 9; j++) {
				if (board[i][j] == 'G')
					solveg(i, j);
				else if (board[i][j] == 'R')
					solver(i, j);
				else if (board[i][j] == 'C')
					solvec(i, j);
				else if (board[i][j] == 'H')
					solveh(i, j);
			}
		}
		
		if (bgx - 1 > 0 && board[bgx - 1 - 1][bgy - 1] == '0') flag = true;
		if (bgx + 1 < 4 && board[bgx + 1 - 1][bgy - 1] == '0') flag = true;
		if (bgy - 1 > 3 && board[bgx - 1][bgy - 1 - 1] == '0') flag = true;
		if (bgy + 1 < 7 && board[bgx - 1][bgy + 1 - 1] == '0') flag = true;
		if (flag) printf("NO\n");
		else {
			if (bgx - 1 > 0 && board[bgx - 1 - 1][bgy - 1] != 'X') {
				char c = board[bgx - 1 - 1][bgy - 1];
				board[bgx - 1 - 1][bgy - 1] = '0';
				board[bgx - 1][bgy - 1] = '0';
				reset();
				push();
				if (board[bgx - 1 - 1][bgy - 1] != 'X') {
					flag = true;
					goto B;
				}
				else {
					board[bgx - 1 - 1][bgy - 1] = c;
					board[bgx - 1][bgy - 1] = 'B';
				}
			}
			if (bgx + 1 < 4 && board[bgx + 1 - 1][bgy - 1] != 'X') {
				char c = board[bgx + 1 - 1][bgy - 1];
				board[bgx + 1 - 1][bgy - 1] = '0';
				board[bgx - 1][bgy - 1] = '0';
				reset();
				push();
				if (board[bgx + 1 - 1][bgy - 1] != 'X') {
					flag = true;
					goto B;
				}
				else {
					board[bgx + 1 - 1][bgy - 1] = c;
					board[bgx - 1][bgy - 1] = 'B';
				}
			}
			if (bgy - 1 > 3 && board[bgx - 1][bgy - 1 - 1] != 'X') {
				char c = board[bgx - 1][bgy - 1 - 1];
				board[bgx - 1][bgy - 1 - 1] = '0';
				board[bgx - 1][bgy - 1] = '0';
				reset();
				push();
				if (board[bgx - 1][bgy - 1 - 1] != 'X') {
					flag = true;
					goto B;
				}
				else {
					board[bgx - 1][bgy - 1 - 1] = c;
					board[bgx - 1][bgy - 1] = 'B';
				}
			}
			if (bgy + 1 < 7 && board[bgx - 1][bgy + 1 - 1] != 'X') {
				char c = board[bgx - 1][bgy + 1 - 1];
				board[bgx - 1][bgy + 1 - 1] = '0';
				board[bgx - 1][bgy - 1] = '0';
				reset();
				push();
				if (board[bgx - 1][bgy + 1 - 1] != 'X') {
					flag = true;
					goto B;
				}
				else {
					board[bgx - 1][bgy + 1 - 1] = c;
					board[bgx - 1][bgy - 1] = 'B';
				}
			}
			B:
			if (flag) printf("NO\n");
			else printf("YES\n");

		}
	A:	
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 9; j++)
				board[i][j] = '0';
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值