POJ 1696 Space Ant (polar sort)

Source: Internet
Author: User

Topic links

Space Ant
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3219 Accepted: 2059

Description

The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down a ant-like creature in the planet Y1999 and called it M11. It has only one eye on the left side of its head and just three feet all on the right side of its body and suffers from th REE Walking Limitations:
    1. It can not be turn right due to its special body structure.
    2. It leaves a red path while walking.
    3. It hates to pass through a previously red colored path, and never does that.

The pictures transmitted by the Discovery space ship depicts, plants in the Y1999 grow in special points on the planet . Analysis of several thousands of the pictures has resulted in discovering a magic coordinate system governing the Grow PO INTs of the plants. In the coordinate system with X and Y axes, no plants share the same x or Y.
An M11 needs to eat exactly one plant on each day to stay alive. When it eats one plant, it remains there for the rest of the day with no move. Next Day, it looks for another plant to go there and eat it. If it can not reach any other plant it dies by the end of the day. Notice that it can reach a plant in any distance.
The problem is to find a path for a M11 to let it live longest.
Input is a set of (x, y) coordinates of plants. Suppose A with the coordinates (XA, YA) are the plant with the least y-coordinate. M11 starts from point (0,ya) heading towards plant A. Notice, the solution path should not cross itself and all of the Turns should be counter-clockwise. Also Note that the solution is visit more than and plants located on a same straight line.

Input

The first line of the input was M, the number of test cases to be solved (1 <= M <= 10). For each test case, the first line was N, the number of plants in that test case (1 <= n <=), followed by N lines For each plant data. Each plant data consists of three integers:the first number are the unique plant index (1..N), followed by and positive in Tegers x and y representing the coordinates of the plant. Plants is sorted by the increasing order on their indices in the input file. Suppose that the values of coordinates is at most 100.

Output

Output should has one separate line for the solution of each test case. A solution is the number of plants on the solution path, and followed by the indices of visiting plants in the Er of their visits.

Sample Input

2101 4 52 9 83 5 94 1 75 3 26 6 37 10 108 8 19 2 410 7 6141 6 112 11 93 8 74 12 85 9 206 3 27 1 68 2 139 15 110 14 1711 13 1912 5 1813 7 314 10 16

Sample Output

10 8 7 3 4 9 5 6 2 1 1014 9 10 11 5 12 8 7 6 13 4 14 1 3 2

Test instructions

An ant, only to turn left, now gives a lot of points on the plane, solving a kind of walk,
Can make the ant can pass the most points, each vertex of the ant can only pass once, and the path of the walk can not cross.

Analysis:

In fact, these n points can be all on the route, just like a snail.

Then the polar order, each time you select the current point to sort the remaining points, to find a minimum angle,

As the current point. Sort when the angle is the same as the distance from small to large.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineLL __int648 Const intMAXN = -+Ten;9 Const DoubleEPS = 1e-8;Ten using namespacestd; One structnode A { -     intF; -     Doublex, y; the }P[MAXN], TT; - DoubleCross (Node A, node B, node C) - { -    return((b.x-a.x) * (C.Y-A.Y)-(c.x-a.x) * (b.y-a.y)); + } - DoubleDis (Node A, Node B) + { A     returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y)); at } - BOOLCMP (Node A, Node B) - { -     Doubleret; -RET =Cross (TT, A, b); -     if(ret==0)returnDis (TT, a) <dis (TT, b); in     Else -     returnRET >0; to } +  - intMain () the { *     intT, I, N, tmp; $scanf"%d", &t);Panax Notoginseng      while(t--) -     { thescanf"%d", &n); +          for(i =0; I < n; i++) A         { thescanf"%D%LF%LF", &AMP;P[I].F, &p[i].x, &p[i].y); +             if(i==0) -TMP =i; $             Else if(P[i].y<p[tmp].y | | (p[i].y==p[tmp].y&&p[i].x<p[tmp].x)) $TMP =i; -         } - node C; thec = p[0]; p[0] = p[tmp]; P[TMP] =C; -tt = p[0];Wuyicout<<N; thecout<<" "<<p[0].f; -          for(i =1; I < n; i++) Wu         { -Sort (P+i, p+N, CMP); Abouttt =P[i]; $cout<<" "<<p[i].f; -         } -cout<<Endl; -     } A     return 0; +}

POJ 1696Space Ant (polar sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.