快来看看我的80分啊!!!??
查看原帖
快来看看我的80分啊!!!??
1332467
yanhaoming楼主2024/6/29 11:31

80分,求助!!!

# include<bits/stdc++.h>
# define int long long
using namespace std;
struct point {
    int x;
    int y;
    void operator=(int a[2]) {
        x = a[0];
        y = a[1];
    }
    void operator=(point b) {
        x = b.x;
        y = b.y;
    }

};

const int xj[8] = {1, 1, -1, -1, 2, 2, -2, -2};
const int yj[8] = {2, -2, 2, -2, 1, -1, 1, -1};
signed main() {
    point S, E;
    int x, y;
    cin >> y >> x >> S.y >> S.x;
    S.x--;
    S.y--;
    int migong[155][155] {};
    point pts[24111];
    int ptsi = 0;
    pts[ptsi] = S;
    ptsi++;
    migong[S.x][S.y] = -1;
    int flag = 1, cnt = 0;
    migong[S.x][S.y] = 1;
    while (flag) {
        flag = 0;
        vector<point> newp;

        for (int i = 0; i < ptsi; i++) {

            migong[pts[i].x][pts[i].y] = cnt + 1;

            for (int j = 0; j < 8; j++) {
                point wz = pts[i];
                wz.x += xj[j];
                wz.y += yj[j];
                if (wz.x >= 0 && wz.x < x && wz.y >= 0 && wz.y < y && migong[wz.x][wz.y] == 0) {
                    flag = 1;
                    newp.push_back(wz);
                    migong[wz.x][wz.y] = -1;
                }
            }

        }
        ptsi = 0;
        for (int i = 0; i < newp.size(); i++) {
            pts[ptsi] = newp[i];
            ptsi++;
        }
        cnt++;
    }
    for (int i = 0; i < y; i++) {
        for (int j = 0; j < x; j++) {
            printf("%5d", migong[j][i] - 1);
        }
        cout << endl;
    }
    return 0;
}

注:用的是 BFS

2024/6/29 11:31
加载中...