2 条题解

  • -1
    @ 2023-3-6 20:33:10

    /*****************************************
    ±¸×¢£º
    ******************************************/
    #include <queue>
    #include <set>
    #include <math.h>
    #include <stack>
    #include <stdio.h>
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <string.h>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define LL long long
    const int N = 1e4 + 10;
    const int INF = 0x3f3f3f3f;
    struct node {
        int x, y;
    
        node(int xx, int yy) {
            x = xx, y = yy;
        }
    };
    int n, m, x, y;
    queue<node> q;
    int a[N][N];
    int vis[N][N];
    int dx[] = {-2, -2, -1, 1, 2, 2, 1, -1};
    int dy[] = {-1, 1, 2, 2, 1, -1, -2, -2};
    void bfs() {
        q.push(node(x, y));
        a[x][y] = 0;
        vis[x][y] = 1;
        while (!q.empty()) {
            int x = q.front().x;
            int y = q.front().y;
            q.pop();
            for (int i = 0; i < 8; i++) {
                int xx = x + dx[i];
                int yy = y + dy[i];
                if (xx >= 1 && yy >= 1 && xx <= n && yy <= m && !vis[xx][yy]) {
                    a[xx][yy] = a[x][y] + 1;
                    q.push(node(xx, yy));
                    vis[xx][yy] = 1;
                }
            }
        }
    }
    int main() {
        cin >> n >> m >> x >> y;
        memset(a, -1, sizeof a);
        bfs();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                printf("%-5d", a[i][j]);
            }
            cout << endl;
        }
        return 0;
    }
    
    
    
    

    信息

    ID
    2495
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    559
    已通过
    111
    上传者