那我也发篇题解吧(勿喷)
#include<bits/stdc++.h>
#define PII pair<int, int>
#define ft first
#define sd second
using namespace std;
int n; PII Map[15][15]; bool complete;
string player[2], chess[10];
// 向零取整
inline int fix(double x) {
return x > 0 ? floor(x) : ceil(x);
}
// 向无穷取整
inline int unfix(double x) {
return x < 0 ? floor(x) : ceil(x);
}
void init(void) {
player[0] = "red"; player[1] = "blue";
chess[1] = "captain"; chess[2] = "guard";
chess[3] = "elephant"; chess[4] = "horse";
chess[5] = "car"; chess[6] = "duck"; chess[7] = "soldier";
for (int i = 0; i <= 8; i++)
for (int j = 0; j <= 9; j++) Map[i][j] = { 0, -1 };
Map[4][0] = { 1, 0 }; Map[4][9] = { 1, 1 };
Map[3][0] = Map[5][0] = { 2, 0 }; Map[3][9] = Map[5][9] = { 2, 1 };
Map[2][0] = Map[6][0] = { 3, 0 }; Map[2][9] = Map[6][9] = { 3, 1 };
Map[1][0] = Map[7][0] = { 4, 0 }; Map[1][9] = Map[7][9] = { 4, 1 };
Map[0][0] = Map[8][0] = { 5, 0 }; Map[0][9] = Map[8][9] = { 5, 1 };
Map[0][2] = Map[8][2] = { 6, 0 }; Map[0][7] = Map[8][7] = { 6, 1 };
for (int i = 0; i <= 8; i += 2) Map[i][3] = { 7, 0 }, Map[i][6] = { 7, 1 };
}
// 判断是否越界/非己方棋子/已有己方棋子
// 若合法,返回被移动的棋子类型
int judge(int x0, int y0, int x, int y, int type) {
if (complete) return -1;
if (x < 0 || x > 8 || y < 0 || y > 9) return -1;
if (x0 < 0 || x0 > 8 || y0 < 0 || y0 > 9) return -1;
if (Map[x0][y0].sd != type || Map[x][y].sd == type) return -1;
return Map[x0][y0].ft;
}
// 判断是否符合行棋规则
bool judgeCaptain(int x0, int y0, int x, int y) {
if (abs(x - x0) + abs(y - y0) != 1) return false;
return true;
}
bool judgeGuard(int x0, int y0, int x, int y) {
if (abs(x - x0) != 1 || abs(y - y0) != 1) return false;
return true;
}
bool judgeElephant(int x0, int y0, int x, int y) {
if (abs(x - x0) != 2 || abs(y - y0) != 2) return false;
if (Map[x0 + (x - x0) / 2][y0 + (y - y0) / 2].ft) return false;
return true;
}
bool judgeHorse(int x0, int y0, int x, int y) {
if (x == x0 || y == y0) return false;
if (abs(x - x0) + abs(y - y0) != 3) return false;
int xn = x0 + fix((x - x0) / 2.0);
int yn = y0 + fix((y - y0) / 2.0);
if (Map[xn][yn].ft) return false;
return true;
}
bool judgeCar(int x0, int y0, int x, int y) {
if (x != x0 && y != y0) return false;
for (int i = min(x0, x) + 1; i < max(x0, x); i++)
if (Map[i][y].ft) return false;
for (int i = min(y0, y) + 1; i < max(y0, y); i++)
if (Map[x][i].ft) return false;
return true;
}
bool judgeDuck(int x0, int y0, int x, int y) {
int dx = abs(x - x0), dy = abs(y - y0);
if (dx + dy != 5 || abs(dx - dy) != 1) return false;
int xn = x0 + unfix((x - x0) / 2.0);
int yn = y0 + unfix((y - y0) / 2.0);
if (Map[xn][yn].ft) return false;
xn = x0 + fix((xn - x0) / 2.0);
yn = y0 + fix((yn - y0) / 2.0);
if (Map[xn][yn].ft) return false;
return true;
}
bool judgeSoldier(int x0, int y0, int x, int y) {
if (abs(x - x0) > 1 || abs(y - y0) > 1) return false;
return true;
}
// 行棋。若起掉敌方棋子,返回其类型
int move(int x0, int y0, int x, int y, int type) {
if (type == 1 && !judgeCaptain(x0, y0, x, y)) return -1;
if (type == 2 && !judgeGuard(x0, y0, x, y)) return -1;
if (type == 3 && !judgeElephant(x0, y0, x, y)) return -1;
if (type == 4 && !judgeHorse(x0, y0, x, y)) return -1;
if (type == 5 && !judgeCar(x0, y0, x, y)) return -1;
if (type == 6 && !judgeDuck(x0, y0, x, y)) return -1;
if (type == 7 && !judgeSoldier(x0, y0, x, y)) return -1;
int res = Map[x][y].ft;
Map[x][y] = Map[x0][y0]; Map[x0][y0] = { 0, -1 };
if (res == 1) complete = true;
return res;
}
// 判断是否将军
bool check(void) {
int xr = -1, yr = -1, xb = -1, yb = -1;
for (int i = 0; i <= 8; i++)
for (int j = 0; j <= 9; j++)
if (Map[i][j].ft == 1) {
if (Map[i][j].sd == 0) xr = i, yr = j;
else xb = i, yb = j;
}
for (int i = 0; i <= 8; i++)
for (int j = 0; j <= 9; j++) {
int type = Map[i][j].ft, p = Map[i][j].sd;
if (type == 0) continue;
if (p && xr != -1 && yr != -1) {
if (type == 1 && judgeCaptain(i, j, xr, yr)) return true;
if (type == 2 && judgeGuard(i, j, xr, yr)) return true;
if (type == 3 && judgeElephant(i, j, xr, yr)) return true;
if (type == 4 && judgeHorse(i, j, xr, yr)) return true;
if (type == 5 && judgeCar(i, j, xr, yr)) return true;
if (type == 6 && judgeDuck(i, j, xr, yr)) return true;
if (type == 7 && judgeSoldier(i, j, xr, yr)) return true;
}
if (!p && xb != -1 && yb != -1) {
if (type == 1 && judgeCaptain(i, j, xb, yb)) return true;
if (type == 2 && judgeGuard(i, j, xb, yb)) return true;
if (type == 3 && judgeElephant(i, j, xb, yb)) return true;
if (type == 4 && judgeHorse(i, j, xb, yb)) return true;
if (type == 5 && judgeCar(i, j, xb, yb)) return true;
if (type == 6 && judgeDuck(i, j, xb, yb)) return true;
if (type == 7 && judgeSoldier(i, j, xb, yb)) return true;
}
}
return false;
}
int main() {
init(); cin >> n; int now = 0;
while (n--) {
int x0, y0, x, y;
cin >> y0 >> x0 >> y >> x;
int mine = judge(x0, y0, x, y, now);
if (mine == -1) {
puts("Invalid command"); continue;
}
int enemy = move(x0, y0, x, y, mine);
if (enemy == -1) {
puts("Invalid command"); continue;
}
cout << player[now] << ' ' << chess[mine] << ';';
if (!enemy) cout << "NA" << ';';
else cout << player[now ^ 1] << ' ' << chess[enemy] << ';';
if (complete) cout << "no;yes\n";
else {
if (check()) cout << "yes;";
else cout << "no;";
cout << "no\n";
}
now ^= 1;//交换执子
}
return 0;
}
共 1 条回复
TQL%%%%%%%
STO TD ORZ