习题4-1 UVa1589-Xiangqi(WA )

我的思路是,使用两个二维数组chess和check分别记录棋盘上棋子的位置和红棋的攻击范围,再判断黑方将棋的活动区域是否可以被攻击到,如果全都会被攻击,则将死。
记录一下过程中犯的错误:

  1. 如果起始状态黑红将棋在同一条线上并且中间没有其他棋子,这种情况是没有将死的,因为黑方将棋可以“flying general”,直接吃掉红方帥棋。
  2. 車、炮、帥的攻击范围的判断有相似的地方。以車为例,它的攻击范围是整条直线,但一旦遇到其他棋子就要停止。要注意的地方是,这颗棋子所在的地方也是会被車攻击到的,在判断攻击范围时不能忽略。炮、帥同理。
  3. 黑方将棋在移动时是可以吃掉红方棋子的。

题目链接:UVa 1589

由于代码量太多,多次提交报WA后实在没有耐心继续调试了。所以把代码存到这里,日后翻到再做调试:
(如果有好心的大佬看到愿意帮忙找一下问题也是极好的,先在此谢过!!)

#include <iostream>
#include <cstring>
#include <ctype.h>
using namespace  std;
char chess[15][15];
int check[15][15];
int r[15];
int c[15];
int r1[15];
int c1[15];
int flag = 1;
bool face = 0;
bool horse(int x, int y, int a, int b) {   //由于馬要考虑的情况太多了,写出来太冗长
	if (x == 1) {                          //所以改为根据黑方“将”棋的位置来判断馬是否能攻击到它
		if ((((a == x + 1 && b == y - 2) || (a == x + 2 && b == y - 1)) && (!isalpha(chess[x + 1][y - 1]))) || (((a == x + 1 && b == y + 2) || (a == x + 2 && b == y + 1)) && (!isalpha(chess[x + 1][y + 1]))))
			return true;
	}
	else if (x == 2) {
		if ((((a == x + 1 && b == y - 2) || (a == x + 2 && b == y - 1)) && (!isalpha(chess[x + 1][y - 1]))) || (((a == x + 1 && b == y + 2) || (a == x + 2 && b == y + 1)) && (!isalpha(chess[x + 1][y + 1]))))
			return true;
		else if ((a == x - 1 && b == y - 2) && !isalpha(chess[x - 1][y - 1]))
			return true;
		else if ((a == x - 1 && b == y + 2) && !isalpha(chess[x - 1][y + 1]))
			return true;
	}
	else if (x == 3) {
		if ((((a == x + 1 && b == y - 2) || (a == x + 2 && b == y - 1)) && (!isalpha(chess[x + 1][y - 1]))) || (((a == x + 1 && b == y + 2) || (a == x + 2 && b == y + 1)) && (!isalpha(chess[x + 1][y + 1]))))
			return true;
		else if ((((a == x - 1 && b == y - 2) || (a == x - 2 && b == y - 1)) && (!isalpha(chess[x - 1][y - 1]))) || (((a == x - 1 && b == y + 2) || (a == x - 2 && b == y + 1)) && (!isalpha(chess[x - 1][y + 1]))))
			return true;
	}
	return false;
}
void attack(int x, int y, int n) {   //攻击范围的判断函数
	for (int i = 0; i <= n; i++) {
		if (r[i] == x && c[i] == y)
			continue;
		if (chess[r[i]][c[i]] == 'R') {   //車的攻击范围
			if (r[i] + 1 <= 10) {
				for (int j = r[i] + 1; j <= 10; j++) {
					if (!isalpha(chess[j][c[i]]))
						check[j][c[i]] = 1;
					else {
						check[j][c[i]] = 1;
						break;
					}
				}
			}
			if (r[i] - 1 >= 0) {
				for (int j = r[i] - 1; j >= 0; j--) {
					if (!isalpha(chess[j][c[i]]))
						check[j][c[i]] = 1;
					else {
						check[j][c[i]] = 1;
						break;
					}
				}
			}
			if (c[i] - 1 >= 0) {
				for (int j = c[i] - 1; j >= 0; j--) {
					if (!isalpha(chess[r[i]][j]))
						check[r[i]][j] = 1;
					else {
						check[r[i]][j] = 1;
						break;
					}
				}
			}
			if (c[i] + 1 <= 9) {
				for (int j = c[i] + 1; j <= 9; j++) {
					if (!isalpha(chess[r[i]][j]))
						check[r[i]][j] = 1;
					else {
						check[r[i]][j] = 1;
						break;
					}
				}
			}
		}
		if (chess[r[i]][c[i]] == 'G') {   //帥的攻击范围
			if (c[i] == y)
				face = 1;
			for (int j = r[i] - 1; j >= 0; j--) {
				if (!isalpha(chess[j][c[i]])) {
					check[j][c[i]] = 1;
				}
				else {
					if (j == x)
						check[j][c[i]] = 1;
					else
						break;
				}
			}
		}
		if (chess[r[i]][c[i]] == 'C') {   //炮的攻击范围
			int cnt = 0, tip = 0;
			if (r[i] + 1 <= 10)
				for (int j = r[i] + 1; j <= 10; j++) {
					if (isalpha(chess[j][c[i]]) && chess[j][c[i]] != 'G')
						cnt++;
					if (cnt == 0)
						continue;
					if (cnt == 1) {
						int cnt1 = 0;
						while (++j <= 10) {
							if (!isalpha(chess[j][c[i]]))
								check[j][c[i]] = 1;
							else {
								if (cnt1 == 0) {
									cnt1 = 1;
									check[j][c[i]] = 1;
								}
								else {
									tip = 1;
									break;
								}
							}
						}
					}
					if (tip)
						break;
				}
			cnt = 0, tip = 0;   //重置判断标志
			if (r[i] - 1 >= 0)
				for (int j = r[i] - 1; j >= 0; j--) {
					if (isalpha(chess[j][c[i]]) && chess[j][c[i]] != 'B')
						cnt++;
					if (cnt == 0)
						continue;
					if (cnt == 1) {
						int cnt1 = 0;
						while (--j >= 0) {
							if (!isalpha(chess[j][c[i]]))
								check[j][c[i]] = 1;
							else {
								if (cnt1 == 0) {
									cnt1 = 1;
									check[j][c[i]] = 1;
								}
								else {
									tip = 1;
									break;
								}
							}
						}
					}
					if (tip)
						break;
				}
			cnt = 0, tip = 0;
			if (c[i] + 1 <= 9)
				for (int j = c[i] + 1; j <= 9; j++) {
					if (isalpha(chess[r[i]][j]))
						cnt++;
					if (cnt == 0)
						continue;
					if (cnt == 1) {
						int cnt1 = 0;
						while (++j <= 9) {
							if (!isalpha(chess[r[i]][j]))
								check[r[i]][j] = 1;
							else {
								if (cnt1 == 0) {
									cnt1 = 1;
									check[r[i]][j] = 1;
								}
								else {
									tip = 1;
									break;
								}
							}
						}
					}
					if (tip)
						break;
				}
			cnt = 0, tip = 0;
			if (c[i] - 1 >= 0)
				for (int j = c[i] - 1; j >= 0; j--) {
					if (isalpha(chess[r[i]][j]))
						cnt++;
					if (cnt == 0)
						continue;
					if (cnt == 1) {
						int cnt1 = 0;
						while (--j >= 0) {
							if (!isalpha(chess[r[i]][j]))
								check[r[i]][j] = 1;
							else {
								if (cnt1 == 0) {
									cnt1 = 1;
									check[r[i]][j] = 1;
								}
								else {
									tip = 1;
									break;
								}
							}
						}
					}
					if (tip)
						break;
				}
		}
	}
}
bool ischeck(int x, int y, int n) {   //判断黑方“将”棋是否被将死
	int hor = 0;
	if (face) {
		bool facing = 1;   //判断输入的状态下黑红双方将棋是否能“见面”
		for (int j = x + 1; j <= 10; j++) {
			if ((isalpha(chess[j][y])) && chess[j][y] != 'G') {
				facing = 0;
				break;
			}
		}
		if (facing)
			return false;
	}
	for (int i = 0; i < n; i++) {   //统计棋盘上馬的数量,根据数量计算是否将死
		if (chess[r[i]][c[i]] != 'H')
			continue;
		else {
			r1[hor] = r[i];
			c1[hor] = c[i];
			hor++;
		}
	}
	if (!hor) {   //如果棋盘上没有馬,则不必调用horse函数
		if (x == 2 && y == 5) {
			if ((check[2][4] != 1) || (check[2][6] != 1) || (check[1][5] != 1) || (check[3][5] != 1))
				return false;
		}
		else if (x == 2) {
			if ((check[1][y] != 1) || (check[3][y] != 1))
				return false;
		}
		else if (y == 5) {
			if ((check[x][4] != 1) || (check[x][6] != 1))
				return false;
		}
		else {
			if ((check[2][y] != 1) || (check[x][5] != 1))
				return false;
		}
		return true;
	}
	else {
		while (hor--) {
			if (x == 2 && y == 5) {
				if (((check[2][4] != 1) && (!horse(2, 4, r1[hor], c1[hor]))) || ((check[2][6] != 1) && (!horse(2, 6, r1[hor], c1[hor]))) || ((check[1][5] != 1) && (!horse(1, 5, r1[hor], c1[hor]))) || ((check[3][5] != 1) && (!horse(1, 5, r1[hor], c1[hor]))))
					return false;
			}
			else if (x == 2) {
				if (((check[1][y] != 1) && (!horse(1, y, r1[hor], c1[hor]))) || ((check[3][y] != 1) && (!horse(3, y, r1[hor], c1[hor]))))
					return false;
			}
			else if (y == 5) {
				if (((check[x][4] != 1) && (!horse(x, 4, r1[hor], c1[hor]))) || ((check[x][6] != 1) && (!horse(x, 6, r1[hor], c1[hor]))))
					return false;
			}
			else {
				if (((check[2][y] != 1) && (!horse(2, y, r1[hor], c1[hor]))) || ((check[x][5]) != 1) && (!horse(x, 5, r1[hor], c1[hor])))
					return false;
			}
			return true;
		}
	}
	return false;
}
int main() {
	int n, a, b;
	char ch;
	memset(chess, '0', sizeof(chess));
	memset(check, 0, sizeof(check));
	while (cin >> n && n != 0) {
		int cha = 0, hor = 0, can = 0, m = 0, p = n;
		cin >> a >> b;
		r[m] = a; c[m] = b;
		m++;
		chess[a][b] = 'B';
		while (n--) {
			cin >> ch >> a >> b;
			r[m] = a; c[m] = b;
			m++;
			chess[a][b] = ch;
		}
		attack(r[0], c[0], p);
		/*for (int i = 1; i < 11; i++) {
			for (int j = 1; j < 10; j++) {
				cout << chess[i][j];
			}
			cout << endl;
		}
		cout << endl;
		for (int i = 1; i < 11; i++) {
			for (int j = 1; j < 10; j++) {
				cout << check[i][j];
			}
			cout << endl;
		}*/
		bool flag = ischeck(r[0], c[0], p);
		if (flag)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
		memset(chess, '0', sizeof(chess));
		memset(check, 0, sizeof(check));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值