当前位置: 首页 > article >正文

算法训练营|图论第11天 Floyd算法 A*算法

题目:Floyd算法

题目链接:

97. 小明逛公园 (kamacoder.com)

代码:

#include<bits/stdc++.h>
using namespace std;
struct Edge {
	int to;
	int val;
	Edge(int t, int w) :to(t), val(w) {}
};
int main() {
	int n, m;
	cin >> n >> m;
	int p1, p2, val;
	vector<vector<vector<int>>>grid(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, 10005)));
	for (int i = 0; i < m; i++) {
		cin >> p1 >> p2 >> val;
		grid[p1][p2][0] = val;
		grid[p2][p1][0] = val;
	}
	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;
	}
}

题目:A*算法 

题目链接:

127. 骑士的攻击 (kamacoder.com)

代码:

#include<bits/stdc++.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;
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;
				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;
}


http://www.kler.cn/news/293037.html

相关文章:

  • 【微服务】接口的幂等性怎么设计?
  • Kubernetes 上安装 Jenkins
  • 5、Django Admin后台移除“删除所选”操作
  • 问:Java异常处理的日常?
  • 民宿小程序开发制作,开发优势分析
  • 《绝区零》全球累积收入突破1亿美金;《原神》斩获年度最佳手游大奖 | 手游和应用出海资讯
  • Linux 进程概念
  • Java笔试面试题AI答之JDBC(4)
  • 006-Sleuth(Micrometer)+ZipKin分布式链路追踪
  • Swift 运算符
  • 在 “Label Studio” 和 “Android Studio” 中,“studio”的含义
  • Signac R|如何合并多个 Seurat 对象 (2)
  • 联蔚盘云亮相CDIE消费品行业峰会
  • React 全屏问题解决方案
  • 8. GIS数据分析师岗位职责、技术要求和常见面试题
  • 计算polydata相交
  • 【数据结构算法经典题目刨析(c语言)】使用数组实现循环队列(图文详解)
  • Opencv中的直方图(3)直方图比较函数compareHist()的使用
  • 原码、反码、补码及用途
  • 微信小程序开发,使用神卓互联内网穿透做公网地址回调的教程
  • python测试开发基础---线程和进程的概念
  • pytorch初始化张量并填充随机整数值
  • 【Linux详解】命令行参数|环境变量
  • OpenAI SORA团队负责人 通往智能的方式 报告笔记
  • 网络层 V(IPv6)【★★★★★★】
  • k8s-pod 实战三 (Liveness Probe 和 Readiness Probe 详细分析)
  • Stage 模型应用程序包的结构
  • Java设计模式【命令模式】-行为型
  • 国内领先线上运动平台:如何借助AI技术实现业务腾飞与用户体验升级
  • HarmonyOS开发实战( Beta5版)合理使用动画丢帧规范实践