0分求助大佬
查看原帖
0分求助大佬
780641
WD2c0mP楼主2023/1/22 14:30

0分求助大佬!

#include <bits/stdc++.h>
#define ts for (int k = 1; k <= 10; k ++) {for (int m = 1; m <= 9; m ++) {cout << qp[k][m] << " ";}cout << endl;}
using namespace std;
string s;
int qp[13][14], x, y, col; //1车2马3象4士5王6鸭7兵
bool over = false;
int dx2[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy2[8] = {1, 2, 2, 1, -1, -2, -2, -1};

int dx3[4] = {-2, 2, 2, -2};
int dy3[4] = {2, 2, -2, -2};

int dx4[4] = {-1, 1, 1, -1};
int dy4[4] = {1, 1, -1, -1};

int dx5[4] = {-1, 0, 1, 0};
int dy5[4] = {0, 1, 0, -1};

int dx6[8] = {-3, -2, 2, 3, 3, 2, -2, -3};
int dy6[8] = {2, 3, 3, 2, -2, -3, -3, -2};

int dx7[8] = {-1, 0, 1, 0, 1, 1, -1, -1};
int dy7[8] = {0, 1, 0, -1, 1, -1, -1, 1};
string nam[8] = {" ", "car", "horse", "elephant", "guard", "captain", "duck", "soldier"};
void init() {
	qp[1][1] = qp[1][9] = 1;
	qp[10][1] = qp[10][9] = -1;
	qp[1][2] = qp[1][8] = 2;
	qp[10][2] = qp[10][8] = -2;
	qp[1][3] = qp[1][7] = 3;
	qp[10][3] = qp[10][7] = -3;
	qp[1][4] = qp[1][6] = 4;
	qp[10][4] = qp[10][6] = -4;
	qp[1][5] = 5;
	qp[10][5] = -5;
	qp[3][1] = qp[3][10] = 6;
	qp[8][1] = qp[8][10] = -6;
	qp[4][1] = qp[4][3] = qp[4][5] = qp[4][7] = qp[4][9] = 7;
	qp[7][1] = qp[7][3] = qp[7][5] = qp[7][7] = qp[7][9] = -7;
}
bool ok1(int sx, int sy, int ex, int ey) {
	int thi = qp[ex][ey];
	if (sx == ex) {
		for (int i = sy + 1; i < ey; i ++) {
			if (qp[sx][i] != 0) return 0;
		}
		return col == 1 ? thi < 0 : thi > 0;
	}
	if (sy == ey) {
		for (int i = sx + 1; i < ex; i ++) {
			if (qp[i][sy] != 0) return 0;
		}
		return col == 1 ? thi < 0 : thi > 0;
	}
	return 0;
}
bool ok2(int sx, int sy, int ex, int ey) {
	for (int i = 0; i < 8; i ++) {
		if (sx + dx2[i] == ex && sy + dy2[i] == ey) {
			int thi = qp[sx + dx2[i]][sy + dy2[i]];
			if (dx2[i] == 2)
				if (!qp[sx + 1][sy])
					if (col == 1 ? thi < 0 : thi > 0) return 1;
			if (dx2[i] == -2)
				if (!qp[sx - 1][sy])
					if (col == 1 ? thi < 0 : thi > 0) return 1;
			if (dy2[i] == 2)
				if (!qp[sx][sy + 1])
					if (col == 1 ? thi < 0 : thi > 0) return 1;
			if (dy2[i] == -2)
				if (!qp[sx][sy - 1])
					if (col == 1 ? thi < 0 : thi > 0) return 1;
		}
	}
	return false;
}
bool ok3(int sx, int sy, int ex, int ey) {
	for (int i = 0; i < 4; i ++) {
		if (sx + dx3[i] == ex && sy + dy3[i] == ey) {
			int thi = qp[sx + dx3[i]][sy + dy3[i]];
			if (sx + dx3[0] == ex && sy + dy3[0] == ey) {
				if (qp[sx - 1][sy + 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (sx + dx3[1] == ex && sy + dy3[1] == ey) {
				if (qp[sx + 1][sy + 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (sx + dx3[2] == ex && sy + dy3[2] == ey) {
				if (qp[sx + 1][sy - 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (sx + dx3[3] == ex && sy + dy3[3] == ey) {
				if (qp[sx - 1][sy - 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
		}
	}
	return false;
}
bool ok4(int sx, int sy, int ex, int ey) {
	for (int i = 0; i < 4; i ++) {
		int thi = qp[sx + dx4[i]][sy + dy4[i]];
		if (sx + dx4[i] == ex && sy + dy4[i] == ey) {
			//if (thi == 0) cout << "is OK" << endl;
			if (thi == 0) return 1;
			if (col == 1 ? thi < 0 : thi > 0) return 1;
		}
	}
	return 0;
}
bool ok5(int sx, int sy, int ex, int ey) {
	for (int i = 0; i < 4; i ++) {
		int thi = qp[sx + dx5[i]][sy + dy5[i]];
		if (sx + dx5[i] == ex && sy + dy5[i] == ey) {
			if (thi == 0) return 1;
			if (col == 1 ? thi < 0 : thi > 0) return 1;
		}
	}
	return 0;
}
bool ok6(int sx, int sy, int ex, int ey) {
	for (int i = 0; i < 8; i ++) {
		if (sx + dx6[i] == ex && sy + dy6[i] == ey) {
			int thi = qp[sx + dx6[i]][sy + dy6[i]];
			if (i == 0) {
				if (qp[sx - 2][sy + 1] || qp[sx - 1][sy]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (i == 1) {
				if (qp[sx - 1][sy + 2] || qp[sx][sy + 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (i == 2) {
				if (qp[sx + 1][sy + 2] || qp[sx][sy + 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (i == 3) {
				if (qp[sx + 2][sy + 1] || qp[sx + 1][sy]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (i == 4) {
				if (qp[sx + 2][sy - 1] || qp[sx + 1][sy]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (i == 5) {
				if (qp[sx + 1][sy - 2] || qp[sx][sy - 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (i == 6) {
				if (qp[sx - 1][sy - 2] || qp[sx][sy - 1]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
			if (i == 7) {
				if (qp[sx - 2][sy - 1] || qp[sx - 1][sy]) return 0;
				if (col == 1 ? thi < 0 : thi > 0) return 1;
				if (thi == 0) return 1;
			}
		}
	}
	return 0;
}
bool ok7(int sx, int sy, int ex, int ey) {
	for (int i = 0; i < 8; i ++) {
		int thi = qp[sx + dx7[i]][sy + dy7[i]];
		if (sx + dx7[i] == ex && sy + dy7[i] == ey) {
			if (thi == 0) return 1;
			if (col == 1 ? thi < 0 : thi > 0)return 1;
		}
	}
	return 0;
}
bool is_valid(int sx, int sy, int ex, int ey) {
	//cout << qp[sx][sy] << endl;
	if (qp[sx][sy] == 0 || over) return 0;
	x = qp[sx][sy], col = qp[sx][sy] > 0 ? 1 : 0; // col:蓝 0 红 1
	if (x == 0) return 0;
	if (abs(x) == 1) return ok1(sx, sy, ex, ey);
	if (abs(x) == 2) return ok2(sx, sy, ex, ey);
	if (abs(x) == 3) return ok3(sx, sy, ex, ey);
	if (abs(x) == 4) return ok4(sx, sy, ex, ey);
	if (abs(x) == 5) return ok5(sx, sy, ex, ey);
	if (abs(x) == 6) return ok6(sx, sy, ex, ey);
	if (abs(x) == 7) return ok7(sx, sy, ex, ey);
	return 0;
}
bool is_jiang() {
	for (int i = 1; i <= 10; i ++) {
		for (int j = 1; j <= 9; j ++) {
			for (int k = 1; k <= 10; k ++) {
				for (int m = 1; m <= 9; m ++) {
					if (i == k && j == m) continue;
					if (is_valid(i, j, k, m) && abs(qp[k][m]) == 5) return 1;
				}
			}
		}
	}
	return 0;
}
void move(int sx, int sy, int ex, int ey) {
	x = qp[sx][sy], y = qp[ex][ey];
	if (x < 0) cout << "blue ";
	else cout << "red ";
	cout << nam[abs(x)] << ";";
	if (y == 0) cout << "NA;";
	else {
		if (y < 0) cout << "blue ";
		else cout << "red ";
		cout << nam[abs(y)] << ";";
		if (abs(y) == 5) over = 1;
	}
	qp[ex][ey] = qp[sx][sy];
	qp[sx][sy] = 0;
	if (is_jiang()) cout << "yes;";
	else cout << "no;";
	if (over) cout << "yes";
	else cout << "no";
}
int main() {
	init();
	int T, turn = 1;
	cin >> T;
	while (T --) {
		int sx, sy, ex, ey;
		cin >> sx >> sy >> ex >> ey;
		sx ++, sy ++, ex ++, ey ++;
		if (!is_valid(sx, sy, ex, ey)) {
			cout << "Invalid command" << endl;
			continue;
		}
		if (turn == 1) {
			if (qp[sx][sy] < 0) {
				cout << "Invalid command" << endl;
				continue;
			}
		}
		if (turn == 0) {
			if (qp[sx][sy] > 0) {
				cout << "Invalid command" << endl;
				continue;
			}
		}
		if (turn) turn = 0;
		else turn = 1;
		move(sx, sy, ex, ey);
		cout << endl;
	}
	return 0;
}
2023/1/22 14:30
加载中...