1 条题解

  • 1
    @ 2022-7-20 9:52:31
    #include <bits/stdc++.h>
    using namespace std;
    typedef pair<int, int> pii;
    #define fi first
    #define se second
    #define mp make_pair
    const int MAXN = 110;
    const int dx[12] = {-2, -2, -2, -2, -1, -1, 1, 1, 2, 2, 2, 2};
    const int dy[12] = {-2, -1, 1, 2, -2, 2, -2, 2, -2, -1, 1, 2};
    int xa, xb, ya, yb, dis[MAXN][MAXN], cnt;
    bool vis[MAXN][MAXN];
    queue<pii> que;
    bool is_in(int x, int y)
    {
    return x >= 1 && x <= 100 && y >= 1 && y <= 100;
    }
    int main()
    {
    cin >> xa >> ya >> xb >> yb;
    dis[1][1] = 0;
    vis[1][1] = true;
    que.push(mp(1, 1));
    while (!que.empty() && cnt < 2) {
    pii cur = que.front();
    que.pop();
    int a = cur.fi, b = cur.se;
    for (int i = 0; i < 12; i++) {
    int x = a + dx[i];
    int y = b + dy[i];
    if (is_in(x, y) && !vis[x][y]) {
    vis[x][y] = true;
    dis[x][y] = dis[a][b] + 1;
    if ((x == xa && y == ya) || (x == xb && y == yb))
    cnt++;
    que.push(mp(x, y));
    }
    }
    }
    cout << dis[xa][ya] << endl << dis[xb][yb] << endl;
    return 0;
    }
    
    • 1

    信息

    ID
    1910
    时间
    1000ms
    内存
    256MiB
    难度
    1
    标签
    递交数
    67
    已通过
    54
    上传者