专栏文章
题解:P3843 [TJOI2007] 迷路
P3843题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @miqbimz5
- 此快照首次捕获于
- 2025/12/04 02:05 3 个月前
- 此快照最后确认于
- 2025/12/04 02:05 3 个月前
题意
题面里没有难懂的部分。
给定两个人的行动路线,要求出两人离得最近时两人的距离。
思路
因为 数据很小,直接暴力枚举即可。
代码
CPP#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
// 计算两点之间的距离
double getDis(int s0, int s1, int s2, int s3) {
return std::sqrt((s2 - s0) * (s2 - s0) + (s3 - s1) * (s3 - s1));
}
// 处理输入并填充 num 向量和计算总步数
void processInput(int& totalSteps, std::vector<std::pair<int, int>>& num) {
int n;
std::cin >> n;
while (n--) {
int tpa;
char tpb;
std::cin >> tpa >> tpb;
int tpc = (tpa < 0)? -1 : 1;
tpa = std::abs(tpa);
totalSteps += tpa;
if (tpb == 'Y') {
for (int i = 0; i < tpa; ++i) {
num.emplace_back(0, tpc);
}
} else {
for (int i = 0; i < tpa; ++i) {
num.emplace_back(tpc, 0);
}
}
}
}
int main() {
int s[4];
int t[2] = {0, 0};
std::vector<std::pair<int, int>> num[2];
double ans = 1e12;
// 读取起点信息并处理输入
std::cin >> s[0] >> s[1];
processInput(t[0], num[0]);
// 读取终点信息并处理输入
std::cin >> s[2] >> s[3];
processInput(t[1], num[1]);
// 计算最大公约数
int Time = std::__gcd(t[0], t[1]);
// 初始化最小距离
ans = getDis(s[0], s[1], s[2], s[3]);
// 模拟移动过程,更新最小距离
for (int i = 0; i < t[0] * t[1] / Time; ++i) {
int tpa = i % t[0];
int tpb = i % t[1];
s[0] += num[0][tpa].first;
s[1] += num[0][tpa].second;
s[2] += num[1][tpb].first;
s[3] += num[1][tpb].second;
ans = std::min(ans, getDis(s[0], s[1], s[2], s[3]));
}
// 输出结果,保留两位小数
std::cout << std::fixed << std::setprecision(2) << ans << std::endl;
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...