题目大概是:中国象棋棋盘(10×9)上有一只马和另外一个棋子,输入马和这颗棋子的坐标,输出马能够跳到的所有位置(先行后列,升序输出),要考虑马脚被绊住和马跳出棋盘的情况
然后我的代码可能是对的,但是有点不满意,希望有人帮忙优化
#include <stdio.h>
#include <stdlib.h>
#define DIRECTIONS 4
#define SITUATIONS 2
#define ROWS 10
#define COLS 9
int dlocation[DIRECTIONS][SITUATIONS][2] = {{{-2, -1}, {-2, 1}},
{{-1, 2}, {1, 2}},
{{2, 1}, {2, -1}},
{{1, -2}, {-1, -2}}}; //dlocation记录往每个方向走的2种情况下x、y分别的变化量
struct location{
int row;
int col;
}; //记录x和y坐标
int cal_direct(struct location horse, struct location obstacle); //用于计算哪个方向上马脚会被绊住,若无则返回-1
int board[ROWS + 1][COLS + 1];
int main(void) {
struct location horse, obstacle;
scanf("%d %d", &horse.row, &horse.col);
scanf("%d %d", &obstacle.row, &obstacle.col);
int direction = cal_direct(horse, obstacle);
for (int direct = 0; direct < DIRECTIONS; ++direct) { //遍历4个方向
if (direct != direction) { //如果该方向上马脚未被绊住
for (int situation = 0; situation < SITUATIONS; ++situation) { //遍历每个方向上的两种可能
int new_row = horse.row+ dlocation[direct][situation][0];
int new_col = horse.col+ dlocation[direct][situation][1];
if (new_row >= 1 && new_row <= ROWS && new_col >= 1 && new_col <= COLS) {
board[new_row][new_col] = 1;
}
}
}
}
for (int i = 1; i <= ROWS; ++i) {
for (int j = 1; j <= COLS; ++j) {
if (board[i][j]) {
printf("%d %d\n", i, j); //输出
}
}
}
return 0;
}
int cal_direct(struct location horse, struct location obstacle) {
int direction = -1; //direction为马脚被绊住的方向,默认-1
int x = horse.row- obstacle.row;
int y = horse.col- obstacle.col;
if (abs(x) + abs(y) == 1) { //计算曼哈顿距离,若为1则马脚会被绊住
direction = !y * (x + 1) + !x * (y + 2); //有点抽象,总之会让(-1, 0), (0, 1), (1, 0), (0, -1)分别返回(0, 1, 2, 3)
}
return direction;
}

然后我的代码可能是对的,但是有点不满意,希望有人帮忙优化
#include <stdio.h>
#include <stdlib.h>
#define DIRECTIONS 4
#define SITUATIONS 2
#define ROWS 10
#define COLS 9
int dlocation[DIRECTIONS][SITUATIONS][2] = {{{-2, -1}, {-2, 1}},
{{-1, 2}, {1, 2}},
{{2, 1}, {2, -1}},
{{1, -2}, {-1, -2}}}; //dlocation记录往每个方向走的2种情况下x、y分别的变化量
struct location{
int row;
int col;
}; //记录x和y坐标
int cal_direct(struct location horse, struct location obstacle); //用于计算哪个方向上马脚会被绊住,若无则返回-1
int board[ROWS + 1][COLS + 1];
int main(void) {
struct location horse, obstacle;
scanf("%d %d", &horse.row, &horse.col);
scanf("%d %d", &obstacle.row, &obstacle.col);
int direction = cal_direct(horse, obstacle);
for (int direct = 0; direct < DIRECTIONS; ++direct) { //遍历4个方向
if (direct != direction) { //如果该方向上马脚未被绊住
for (int situation = 0; situation < SITUATIONS; ++situation) { //遍历每个方向上的两种可能
int new_row = horse.row+ dlocation[direct][situation][0];
int new_col = horse.col+ dlocation[direct][situation][1];
if (new_row >= 1 && new_row <= ROWS && new_col >= 1 && new_col <= COLS) {
board[new_row][new_col] = 1;
}
}
}
}
for (int i = 1; i <= ROWS; ++i) {
for (int j = 1; j <= COLS; ++j) {
if (board[i][j]) {
printf("%d %d\n", i, j); //输出
}
}
}
return 0;
}
int cal_direct(struct location horse, struct location obstacle) {
int direction = -1; //direction为马脚被绊住的方向,默认-1
int x = horse.row- obstacle.row;
int y = horse.col- obstacle.col;
if (abs(x) + abs(y) == 1) { //计算曼哈顿距离,若为1则马脚会被绊住
direction = !y * (x + 1) + !x * (y + 2); //有点抽象,总之会让(-1, 0), (0, 1), (1, 0), (0, -1)分别返回(0, 1, 2, 3)
}
return direction;
}



