MLE求调!!!
  • 板块P1443 马的遍历
  • 楼主wky_wsy
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/11/22 21:30
  • 上次更新2024/11/23 01:36:40
查看原帖
MLE求调!!!
1323415
wky_wsy楼主2024/11/22 21:30

还是用dfs打bfs

#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[407][407];
int n,m,x,y;
void dfs(int x,int y,int k){
    if(a[x][y]==-1){
        a[x][y]=k;
    }
    else if(a[x][y]>k){
        a[x][y]=k;
    }
    if(x-1>=1&&y+2<=m) dfs(x-1,y+2,k+1);
    if(x+1<=n&&y+2<=m) dfs(x+1,y+2,k+1);
    if(x-1>=1&&y-2>=1) dfs(x-1,y-2,k+1);
    if(x+1<=n&&y-2>=1) dfs(x+1,y-2,k+1);
    if(x+2<=n&&y+1<=m) dfs(x+2,y+1,k+1);
    if(x+2<=n&&y-1>=1) dfs(x+2,y-1,k+1);
    if(x-2>=1&&y-1>=1) dfs(x-2,y-1,k+1);
    if(x-2>=1&&y+1<=m) dfs(x-2,y+1,k+1);
}
signed main(){
    cin.tie(0);
    cout.tie(0);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            a[i][j]=-1;
        }
    }
    cin>>n>>m>>x>>y;
    a[x][y]=0;
    dfs(x,y,0);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<a[i][j]<<' ';
        }
        putchar('\n');
    }
}
2024/11/22 21:30
加载中...