代码随想录算法训练营第六十二天 | 图论part11

97. 小明逛公园

cpp 复制代码
#include <iostream>
#include <vector>
#include <climits>
#include <fstream>

using namespace std;

void floyd(vector<vector<vector<int>>>& grid) {
    int n = grid.size() - 1;
    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 main() {
    ifstream infile("input.txt");
    int n, m;
    int u, v, w;
    cin >> n >> m;
    // grid[i][j][k]代表从i到j且只经过1-k作为中间节点的最小距离
    vector<vector<vector<int>>> grid(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 10005)));
    while (m--) {
        cin >> u >> v >> w;
        grid[u][v][0] = w;
        grid[v][u][0] = w;
    }
    floyd(grid);
    int q;
    cin >> q;
    int start, end;
    while (q--) {
        cin >> start >> end;
        if (grid[start][end][n] != 10005) cout << grid[start][end][n] << endl;
        else cout << -1 << endl;
    }
}

127. 骑士的攻击

cpp 复制代码
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

int dir[8][2] = { -2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2 };

struct Knight {
	int x, y;
	int f, g, h;
	Knight(){}
	Knight(int x, int y) : x(x), y(y) {}
	bool operator < (const Knight& k) const { return k.f < f;}
};

int Heuristic(Knight &k, int b1, int b2) {
	return (k.x - b1) * (k.x - b1) + (k.y - b2) * (k.y - b2);
}

int astar(Knight& start, int b1, int b2) {
	int moves[1001][1001] = { 0 };
	priority_queue<Knight> que;
	que.push(start);
	while (que.size()) {
		Knight k = que.top();
		Knight next;
		que.pop();
		if (k.x == b1 && k.y == b2) return moves[b1][b2];
		for (int i = 0; i < 8; ++i) {
			next.x = k.x + dir[i][0];
			next.y = k.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[k.x][k.y] + 1;

				// 开始计算F
				next.g = k.g + 5; // 统一不开根号,这样可以提高精度,马走日,1 * 1 + 2 * 2 = 5
				next.h = Heuristic(next, b1, b2);
				next.f = next.g + next.h;
				que.push(next);
			}
		}
	}
	return -1;
}

int main() {
	ifstream infile("input.txt");
	int n;
	cin >> n;
	while (n--) {
		int a1, a2, b1, b2;
		cin >> a1 >> a2 >> b1 >> b2;
		Knight start(a1, a2);
		start.g = 0;
		start.h = Heuristic(start, b1, b2);
		start.f = start.g + start.h;
		cout << astar(start, b1, b2) << endl;
	}
	infile.close();
}
相关推荐
bIo7lyA8v1 天前
算法复杂度评估的实验统计方法与可视化的技术8
算法
李老师讲编程1 天前
中国电子学会图形化2020.12月Scratch三级考级题
算法·scratch·信息学奥赛·图形化编程·scratch素材
退休倒计时1 天前
【每日一题】LeetCode 53. 最大子数组和 TypeScript
数据结构·算法·leetcode·typescript
旖-旎1 天前
FloodFill(图像渲染)(1)
c++·算法·深度优先·力扣
戴西软件1 天前
戴西 DLM 许可授权管理系统:破解无网络环境下工业软件授权难题,助力制造企业降本增效
网络·人工智能·python·深度学习·程序人生·算法·制造
2601_961875241 天前
法考资料2026|全套|资料已整理
数据结构·算法·链表·贪心算法·eclipse·线性回归·动态规划
无限码力1 天前
美团研发岗 4月18号笔试真题 - 坐标
算法·美团笔试真题·美团笔试题·美团研发岗笔试题·美团研发岗4月18号真题
有点。1 天前
C++倍增法(练习题)
c++·算法
智者知已应修善业1 天前
【51单片机8位数码管同时倒计时从9999】2024-1-25
c++·经验分享·笔记·算法·51单片机