题解:P3843 [TJOI2007] 迷路 - 洛谷专栏
。
帮我看一下有没有问题
P3843 [TJOI2007] 迷路 题解
14 分错因:
有很大可能是看错了题,d 是每个任务总共要走的路程,而两个人每秒只能走一个单位。
思路
因为小 A 和小 B 的运动轨迹是周期性的,会循环,因此我们只需要 \gcd 一下,求出最小公倍数,然后暴力枚举,用两点之间距离公式来计算所有时刻的距离,最后取 \min 并输出就可以了。
两点距离公式:
\sqrt{(x1−x2)×(x1−x2)+(y1−y2)×(y1−y2)}
剩下问题见于代码。
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);//求最大公约数
}
int lcm(int a,int b){
return a/gcd(a,b)*b;//调用gcd,求最小公倍数
}
void path_read(vector<int>&x_coords,vector<int>&y_coords){//读取轨道坐标
int sx,sy,m;
cin>>sx>>sy>>m;//起始点坐标,指令条数
x_coords.clear();//清空坐标
y_coords.clear();
int current_x=sx;
int current_y=sy;
for(int i=0;i<m;++i){//m条指令
int d;
char c;
cin>>d>>c;
int s=abs(d);
int dir_x=0,dir_y=0;
if(c=='X'){
dir_x=d>0?1:-1;
}
else{
dir_y=d>0?1:-1;
}
for(int j=0;j<s;++j){//s步
current_x+=dir_x;
current_y+=dir_y;
x_coords.push_back(current_x);//将坐标存入
y_coords.push_back(current_y);
}
}
}
int main(){
vector<int>a_x,a_y,b_x,b_y;
path_read(a_x,a_y);//读取小A的轨道
path_read(b_x,b_y);//读取小B的轨道
int ta=a_x.size();//小A的轨道长度
int tb=b_x.size();//小B的轨道长度
if(ta==0||tb==0){//特判,如果轨道长度为0,直接输出0.00
printf("0.00\n");
return 0;
}
int L=lcm(ta,tb);//计算最小公倍数
double min_dist=1e18;//初始化最小距离为1e18
for(int t=0;t<L;++t){//枚举每个时刻
int a_idx=t%ta;//小A的当前位置
int b_idx=t%tb;//小B的当前位置
int dx=a_x[a_idx]-b_x[b_idx];//下一个x坐标
int dy=a_y[a_idx]-b_y[b_idx];//下一个y坐标
double dist=sqrt(dx*dx+dy*dy);//计算距离
min_dist=min(min_dist,dist);//更新最小距离
}
printf("%.2f\n",min_dist);//输出最小距离,保留两位小数,没得说
return 0;
}
帮你改了一下
我把文章改了一下,更离谱了
无法理解
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>503 - 洛谷日爆</title>
<base href="https://cdn.luogu.com.cn/errpage/" />
</head>
<body>
<div class="header">
<div class="max"><img src="logo.png" style="height: 3em;"></div>
</div>
<div style="margin-top: 40px;">
<div class="card max" style="background-image: url('gg1.svg');">
<h1>503 Service Unavailable</h1>
<p>服务目前不可用</p>
<p><small>请稍候刷新重试,但不要发布无意义帖子</small></p>
<p><span class="diag">[2025-04-19T14:50:05+08:00] 71.19.144.199 -> Cloudflare -> CNHAZa.web-i01</span>
</p>
</div>
</div>
<div class="footer max">2013-<span id="year">9999</span>, © 洛谷</div>
<style>
html,
body {
padding: 0;
margin: 0;
font-size: 16px;
background-color: #f2f2f2;
}
h1 {
margin: .25em 0;
font-size: 3.5em;
font-weight: lighter;
}
small {
font-size: .875em;
color: #aaa;
}
p {
font-size: 1em;
margin: .25em 0;
}
.max {
box-sizing: border-box;
margin-left: auto;
margin-right: auto;
width: 100%;
max-width: 1200px;
}
.header {
margin: 0 !important;
width: 100%;
box-sizing: border-box;
background-color: #fff;
box-shadow: 0px 1px 3px rgba(26, 26, 26, 0.1);
padding: .25em 20px;
}
.footer {
text-align: center;
padding: 12px 0;
}
.card {
text-align: center;
background: #fff;
box-shadow: 0px 1.03426px 3.10278px rgba(26, 26, 26, 0.1);
padding-top: .75em;
min-height: 800px;
background-position: bottom;
background-repeat: no-repeat;
background-size: cover;
}
.beian {
color: #999;
text-decoration: none;
}
.diag {
font-size: .75em;
font-family: monospace, monospace, Arial;
color: #ddd;
}
</style>
<script>document.getElementById('year').textContent = new Date().getFullYear();</script>
</body>
</html>
我要成这个HTML代码了