50分求助 不会STL什么的 只会记忆化搜索
查看原帖
50分求助 不会STL什么的 只会记忆化搜索
1144743
pku_lmc楼主2024/5/6 22:56
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
int n, m;
int s[405][405];
int dx[8] = {2,1,-1,-2,-2,-1,1,2};
int dy[8] = {1,2,2,1,-1,-2,-2,-1};
int vis[405][405];
void f(int x, int y, int currs){
    if(currs >= s[x][y]) return;
    if(vis[x][y]) return;
    vis[x][y] = 1;
    s[x][y] = currs;
    for(int i = 0; i < 8; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx>0&&nx<=n&&ny>0&&ny<=m) {
            f(nx,ny,currs+1);
        }
    }
    vis[x][y] = 0;
}
int main(){
    memset(s,0x3f,sizeof(s));
    int x, y;
    cin >> n >> m >> x >> y;
    f(x,y,0);
    for(int i = 1; i <= n; i++){
        if(s[i][1] < 0x3f)cout << s[i][1];
        else cout << -1;
        for(int j = 2; j <= m; j++)
        if(s[i][j] < 0x3f)cout <<' ' <<s[i][j];
        else cout << -1;
        cout << endl;
    }
    return 0;
}
2024/5/6 22:56
加载中...