如题
using namespace std;
const int MAXN = 35;
int n;
int chess[MAXN*10][MAXN*10];
void dfs(int x,int y,int st){
if(chess[x][y] <= st){
return;
}
if(x > MAXN || x < 0 || y > MAXN || y < 0){
return;
}
chess[x][y] = min(chess[x][y],st);
dfs(x+2,y+1,st+1);
dfs(x+2,y-1,st+1);
dfs(x+1,y+2,st+1);
dfs(x+1,y-2,st+1);
dfs(x-1,y+2,st+1);
dfs(x-1,y-2,st+1);
dfs(x-2,y+1,st+1);
dfs(x-2,y-1,st+1);
dfs(x+2,y+2,st+1);
dfs(x+2,y-2,st+1);
dfs(x-2,y+2,st+1);
dfs(x-2,y-2,st+1);
}
int main(){
int x1,y1;
cin >> x1 >> y1;
for(int i = 0;i < MAXN;i++){
for(int j = 0;j < MAXN;j++){
chess[i][j] = 114;
}
}
dfs(x1+10,y1+10,0);
cout << chess[11][11] << endl;
cin >> x1 >> y1;
for(int i = 0;i < MAXN;i++){
for(int j = 0;j < MAXN;j++){
chess[i][j] = 114;
}
}
dfs(x1+10,y1+10,0);
cout << chess[11][11] << endl;
return 0;
}
有个样例是2,2,答案是3,但我手算和这个代码输出的都是2。 考虑到有时候暂时走出棋盘的行为可能更快,因此把所有的下标增加10。