题目链接:https://cn.vjudge.net/problem/CodeForces-1085C
题目大意:给了你三个点,让你建一条路把三个点都连起来,路径尽可能小。
解题思路:构造题,找到中间位置的点,以他做垂线,两边的点水平连接。
#include <bits/stdc++.h>
using namespace std;
set <pair<int, int> >st;
struct node
{
int x, y;
}a[105];
bool cmp(struct node a, struct node b)
{
return a.x > b.x;
}
int main()
{
int i, j, k, n, m;
for(i = 0; i < 3; i++)cin >> a[i].x >> a[i].y;
sort(a, a + 3, cmp);
int l1, l2, r1, r2;
l1 = max(max(a[0].y, a[1].y), a[2].y);
l2 = min(min(a[0].y, a[1].y), a[2].y);
r1 = max(max(a[0].x, a[1].x), a[2].x);
r2 = min(min(a[0].x, a[1].x), a[2].x);
//for(i = 0; i < 3; i++)cout << a[i].x << " " << a[i].y << endl;
for(i = l2; i <= l1; i++)
{
//if(i == a[1].y)continue;
//cout << a[1].x << " " << i << endl;
st.insert(make_pair(a[1].x, i));
}
//cout << l1 << " " << l2 << " " << r1 << " " << r2 << endl;
for(i = r2; i <= a[1].x; i++)
{
//if(i == a[0].x)continue;
st.insert(make_pair(i, a[2].y));
}
for(i = a[1].x; i <= r1; i++)
{
//if(i == a[2].x)continue;
st.insert(make_pair(i, a[0].y));
}
cout << st.size() << endl;
set<pair<int, int> >::iterator it;
for(it = st.begin(); it != st.end(); it++)
{
cout << it -> first << " " << it -> second << endl;
}
return 0;
}
本文提供了一个解决 CodeForces-1085C 题目的算法实现,该题要求构建一条连接三个点的最小路径。通过寻找中间点并构建垂直和水平路径,算法确保了路径长度最短。
255

被折叠的 条评论
为什么被折叠?



