5、9失败了,显示递归太深,求大佬优化```c
#include
#include
#include
#include
#include<string.h>
using namespace std;
int n, m;
int xx, yy;
int s[405][405];
const int dx[8] = { -1,-2,-2,-1,1,2,2,1 };//8个方向
const int dy[8] = { 2,1,-1,-2,2,1,-1,-2 };//8个方向
int num = -1;
void walk(int x, int y) {
if (x<1 || x>n || y<1 || y>m) return;
if (s[x][y] != -1) {
if (++num >= s[x][y]) {
num --;
return;
}
else {//从起点开始走
s[x][y] = num;
}
}
else {
s[x][y] = ++num;
}
for (int i = 0; i < 8; i++) {
if (x == xx && y == yy) {
num = 0;
}
walk(x + dx[i], y + dy[i]);
}
num--;
}
int main() {
cin >> n >> m;
int num = 0; int index = 0;
memset(s, -1, sizeof(int) * 405 * 405);
cin >> xx >> yy;
walk(xx, yy);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
printf("%-5d", s[i][j]);
}
cout << endl;
}
}