用的dfs,不知道为啥,试了好几个数据都不会出现-1,特别奇怪
#include<iostream>
using namespace std;
int n,m,x,y;
int board[405][405];
bool check(int hx,int hy){
if(hx<=n&&hy<=m&&hx>=0&&hy>=0)return 1;
return 0;
}
void dfs(int hx,int hy,int step){
if(step<board[hx][hy]||board[hx][hy]==-1){
board[hx][hy]=step;
if(check(hx-1,hy+2))dfs(hx-1,hy+2,step+1);
if(check(hx+1,hy+2))dfs(hx+1,hy+2,step+1);
if(check(hx-1,hy-2))dfs(hx-1,hy-2,step+1);
if(check(hx+1,hy-2))dfs(hx+1,hy-2,step+1);
if(check(hx-2,hy+1))dfs(hx-2,hy+1,step+1);
if(check(hx-2,hy-1))dfs(hx-2,hy-1,step+1);
if(check(hx+2,hy+1))dfs(hx+2,hy+1,step+1);
if(check(hx+2,hy-1))dfs(hx+2,hy-1,step+1);
}
}
int main(){
for(int i=0;i<405;i++){
for(int j=0;j<405;j++){
board[i][j]=-1;
}
}
cin>>n>>m>>x>>y;
x--;
y--;
dfs(x,y,0);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<board[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
=)
您的电脑 颠佬没有任何问题,我们也不会为你重新启动,且把你的所有隐私上传到网络上=)
完成:-114514%