cpp 复制Markdown 展开题目描述在中国象棋中马走日字形。用 ��ij 表示第 �i 行第 �j 列的格点不考虑别马腿和跳出棋盘外在 ��ij 的马可以跳到 �−2�+1i−2j+1�−2�−1i−2j−1�−1�+2i−1j+2�−1�−2i−1j−2�+1�+2i+1j+2�+1�−2i+1j−2�+2�+1i+2j+1�+2�−1i+2j−1 八个位置。将军抽车是中国象棋中常用的进
#include
bool canCheckmate(int Sx, int Sy, int Cx, int Cy, int Mx, int My) { // Check if the horse can jump to any of the positions that can attack the king int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2}; int dy[8] = {1, -1, 2, -2, 2, -2, 1, -1};
for (int i = 0; i < 8; i++) {
int nx = Mx + dx[i];
int ny = My + dy[i];
if (nx == Sx && ny == Sy) {
// The horse can jump to a position that can attack the king
return true;
}
}
return false;
}
int main() { int Sx, Sy, Cx, Cy, Mx, My; cin >> Sx >> Sy >> Cx >> Cy >> Mx >> My;
bool result = canCheckmate(Sx, Sy, Cx, Cy, Mx, My);
if (result) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
原文地址: https://www.cveoy.top/t/topic/iCmG 著作权归作者所有。请勿转载和采集!