代码随想录算法训练营第六十七天|KMC97 小明逛公园、KMC127 骑士的攻击

题1:

指路:97. 小明逛公园 (kamacoder.com)
思路与代码:
Floyd:
#include<iostream>
#include<vector>
#include<list>

using namespace std;

int main() {
    int n, m, p1, p2, val;
    cin >> n >> m;
    vector<vector<vector<int>>> grid (n + 1, vector<vector<int>> (n + 1,
    vector<int> (n + 1, 10005)));  // 边的最大值是10^4
    for (int i = 0; i < m; i++) {
        cin >> p1 >> p2 >> val;
        grid[p1][p2][0] = val;
        grid[p2][p1][0] = val;  // 双向图
    }
    // 开始Floyd
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                grid[i][j][k] = min (grid[i][j][k - 1], 
                grid[i][k][k - 1] + grid[k][j][k - 1]);
            }
        }
    }
    // 输出
    int z, start, end;
    cin >> z;
    while (z--) {
        cin >> start >> end;
        if (grid[start][end][n] == 10005) cout << -1 << endl;
        else cout << grid[start][end][n] << endl; 
    }
}

 题2:

指路:127. 骑士的攻击 (kamacoder.com)
思路与代码:
BFS宽搜:
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;

    int moves[1001][1001];
    int dir[8][2] = {-2, -1, -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, -2};
    void bfs (int a1, int a2, int b1, int b2) {
        queue<int> q;
        q.push(a1);
        q.push(a2);
        while (!q.empty()) {
            int m = q.front();
            q.pop();
            int n = q.front();
            q.pop();
            if (m == b1 && n == b2) break;
            for (int i = 0; i < 8; i++) {
                int mm = m + dir[i][0];
                int nn = n + dir[i][1];
                if (mm < 1 || mm > 1000 || nn < 1 || nn > 1000) 
                continue;
                if (!moves[mm][nn]) {
                    moves[mm][nn] = moves[m][n] + 1;
                    q.push(mm);
                    q.push(nn);
                }
            }
        }
    }
    int main() {
        int n, a1, a2, b1, b2;
        cin >> n;
        while (n--) {
            cin >> a1 >> a2 >> b1 >> b2;
            memset(moves, 0, sizeof(moves));
            bfs (a1, a2, b1, b2);
            cout << moves[b1][b2] << endl;
        }
        return 0;
    }
 Astar:
#include<iostream>
#include<queue>
#include<string.h>

using namespace std;

int moves[1001][1001];
int dir[8][2] = {-2, -1, -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, -2};
int b1, b2;
// F = G + H
// G = 从起点到该节点路径消耗
// H = 该节点到终点的预估消耗
struct Knight {
    int x, y;
    int g, h, f;
    bool operator < (const Knight & k) const {
        // 重载
        return k.f < f;
    }
};
priority_queue<Knight> que;
int Heuristic (const Knight& k) {  // 欧拉距离
    return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2);
}
void astar (const Knight& k) {
    Knight cur, next;
    que.push(k);
    while (!que.empty()) {
        cur = que.top(); que.pop();
        if (cur.x == b1 && cur.y == b2) 
            break;
            for (int i = 0; i < 8; i++) {
                next.x = cur.x + dir[i][0];
                next.y = cur.y + dir[i][1];
                if (next.x < 1 || next.x > 1000 ||
                next.y < 1 || next.y > 1000) continue;
                if (!moves[next.x][next.y]) {
                    moves[next.x][next.y] = moves[cur.x][cur.y] + 1;
                    // 计算F
                    next.g = cur.g + 5;
                    next.h = Heuristic(next);
                    next.f = next.g + next.h;
                    que.push(next);
                }
            }
        }
    }

    int main() {
        int n, a1, a2;
        cin >> n;
        while (n--) {
            cin >> a1 >> a2 >> b1 >> b2;
            memset(moves, 0, sizeof(moves));
            Knight start;
            start.x = a1;
            start.y = a2;
            start.g = 0;
            start.h = Heuristic(start);
            start.f = start.g + start.h;
            astar(start);
            while (!que.empty()) que.pop();  // 清空
            cout << moves[b1][b2] << endl;
        }
        return 0;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C.G.道枝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值