dfs90pts,bfs30pts

P1443 马的遍历

Maxwell1 @ 2024-10-16 23:24:45

Help me!

DFS:(90ptsTLE)

#include<bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff

const int MAXN = 402;
int arr[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, m;
int si, sj;
int op[8][2] = {
        {-2, -1}, {-2, 1},
    {-1, -2},           {-1, 2},
    {1, -2},            {1, 2},
        {2, -1}, {2, 1}
};

void init() {
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            arr[i][j] = INF;
    memset(vis, 0, sizeof vis);
    //arr[si][sj] = 0;
}

void dfs(int i, int j, int st) {
    if(i < 1 || i > n || j < 1 || j > m || vis[i][j])
        return;
    if(arr[i][j] <= st)
        return;
    //cout << i << " " << j << " " << st << endl;
    arr[i][j] = st;
    vis[i][j] = true;
    for(int ii = 0; ii < 8; ii++) {
        dfs(i + op[ii][0], j + op[ii][1], st + 1);
    }
    vis[i][j] = false;
}

int main() {
    scanf("%d%d%d%d", &n, &m, &si, &sj);
    init();
    dfs(si, sj, 0);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++)
            if(arr[i][j] == INF)
                printf("-1 ");
            else
                printf("%d ", arr[i][j]);
        printf("\n");
    }
    return 0;
}

BFS:(30ptsMLE)

#include<bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff

const int MAXN = 402;
int arr[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, m;
int si, sj;
int op[8][2] = {
        {-2, -1}, {-2, 1},
    {-1, -2},           {-1, 2},
    {1, -2},            {1, 2},
        {2, -1}, {2, 1}
};

void init() {
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            arr[i][j] = INF;
    memset(vis, 0, sizeof vis);
    //arr[si][sj] = 0;
}

struct node {
    int x, y, st;
};
queue<node> q;
void bfs() {
    node f = {si, sj, 0};
    node g;
    q.push(f);
    while(!q.empty()) {
        f = q.front();
        q.pop();
        arr[f.x][f.y] = f.st;
        for(int i = 0; i < 8; i++) {
            g.x = f.x + op[i][0];
            g.y = f.y + op[i][1];
            g.st = f.st + 1;
            if(g.x < 1 || g.x > n || g.y < 1 || g.y > m)
                continue;
            if(arr[g.x][g.y] <= g.st)
                continue;
            q.push(g);
        }
    }
}

int main() {
    scanf("%d%d%d%d", &n, &m, &si, &sj);
    init();
    bfs();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++)
            if(arr[i][j] == INF)
                printf("-1 ");
            else
                printf("%d ", arr[i][j]);
        printf("\n");
    }
    return 0;
}

by lugaoxiang1205 @ 2024-10-19 12:03:04

#include<bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff

const int MAXN = 402;
int arr[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, m;
int si, sj;
int op[8][2] = {
        {-2, -1}, {-2, 1},
    {-1, -2},           {-1, 2},
    {1, -2},            {1, 2},
        {2, -1}, {2, 1}
};

void init() {
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            arr[i][j] = INF;
    memset(vis, 0, sizeof vis);
    //arr[si][sj] = 0;
}

void dfs(int i, int j, int st) {
    if(st>500){
        return;
    }
    if(i < 1 || i > n || j < 1 || j > m || vis[i][j])
        return;
    if(arr[i][j] <= st)
        return;
    //cout << i << " " << j << " " << st << endl;
    arr[i][j] = st;
    vis[i][j] = true;
    for(int ii = 0; ii < 8; ii++) {
        dfs(i + op[ii][0], j + op[ii][1], st + 1);
    }
    vis[i][j] = false;
}

int main() {
    scanf("%d%d%d%d", &n, &m, &si, &sj);
    init();
    dfs(si, sj, 0);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++)
            if(arr[i][j] == INF)
                printf("-1 ");
            else
                printf("%d ", arr[i][j]);
        printf("\n");
    }
    return 0;
}

AC代码


by hsy2024 @ 2024-11-02 10:26:40

去重啊乐子哥


|