HDU6300-Triangle Partition

HDU6300-Triangle Partition

题目:
Chiaki has
3n
3n
points
p
1
,
p
2
,…,
p
3n
p1,p2,…,p3n
. It is guaranteed that no three points are collinear.
Chiaki would like to construct
n
n
disjoint triangles where each vertex comes from the
3n
3n
points.
Input
There are multiple test cases. The first line of input contains an integer
T
T
, indicating the number of test cases. For each test case:
The first line contains an integer
n
n
(
1≤n≤1000
1≤n≤1000
) – the number of triangle to construct.
Each of the next
3n
3n
lines contains two integers
x
i
xi
and
y
i
yi
(

10
9

x
i
,
y
i

10
9
−109≤xi,yi≤109
).
It is guaranteed that the sum of all
n
n
does not exceed
10000
10000
.
Output
For each test case, output
n
n
lines contain three integers
a
i
,
b
i
,
c
i
ai,bi,ci
(
1≤
a
i
,
b
i
,
c
i
≤3n
1≤ai,bi,ci≤3n
) each denoting the indices of points the
i
i
-th triangle use. If there are multiple solutions, you can output any of them.
Sample Input
1
1
1 2
2 3
3 5
Sample Output
1 2 3

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define MAX 8000

struct point
{
    int x;
    int y;
    int num;
};

int main()
{
    int T,n,mina;
    struct point a[MAX];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 0;i < 3 * n;i++)
        {
            cin >> a[i].x >> a[i].y;
            a[i].num = i + 1;
        }

        for(int i = 0;i < 3 * n;i++)
        {
            mina = i;
            for(int j = i + 1;j < 3 * n;j++)
            {
                if(a[j].x < a[mina].x) mina = j;
            }
            swap(a[mina],a[i]);
        }

        for(int i = 0;i < 3 * n;)
        {
            cout << a[i].num << " "<< a[i + 1].num << " " << a[i + 2].num << endl;
            i += 3;
        }
    }
    return 0;
}

题目要求给出3n个点,并且每三个点都不共线。每次要求输出n组三角形的点的索引(1组输出三个点的索引)。这道题的思路是先建立一个结构体,每个结构体都代表一个点,结构体中有点的横坐标,纵坐标,以及点的索引。因为题目要求输出的三角形之间不能重合,所以在这里我将这些点根据横坐标的从小到大进行排序。那么,每次输出的三角形都不会重合。在这里我使用的选择排序(当然了,后来发现sort的耗时更少,所以也可以用sort进行排序),随后进行输出就可以了。

猜你喜欢

转载自blog.csdn.net/qq_41998938/article/details/83586283