MLE求调!!!

P1443 马的遍历

wky_wsy @ 2024-11-22 21:30:20

还是用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');
    }
}

by __assassin_ @ 2024-11-22 21:43:10

你dfs没有设置结束条件


by __assassin_ @ 2024-11-22 21:43:44

@wky_wsy


by wky_wsy @ 2024-11-22 22:17:38

@_assassin
那不是应该 TLE 吗???


by GWOI @ 2024-11-23 13:21:43

@wky_wsy 当然是 bfs


by __assassin_ @ 2024-11-23 13:55:46

@wky_wsy

我想你是dfs没有设置结束条件,创建了很多的 x,y,k变量,导致内存超限而 MLE


|