poj1696/hlg1318 蛋疼的蚂蚁

Space Ant










Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2934   Accepted: 1874

Description

The most exciting space discovery occurred at the
end of the 20th century. In 1999, scientists traced down an 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
three walking limitations: 

  1. It can not turn right due to its special body structure.

  2. It leaves a red path while walking.

  3. It hates to pass over a previously red colored path, and never does
    that.

The pictures transmitted by the Discovery space ship depicts
that plants in the Y1999 grow in special points on the planet. Analysis of
several thousands of the pictures have resulted in discovering a magic
coordinate system governing the grow points of the plants. In this coordinate
system with x and y axes, no two plants share the same x or
y

An M11 needs to eat exactly one plant in 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 an M11 to let
it live longest. 
Input is a set of (x, y) coordinates of plants.
Suppose A with the coordinates (xA, yA) is the plant with the least
y-coordinate. M11 starts from point (0,yA) heading towards plant A. Notice that
the solution path should not cross itself and all of the turns should be
counter-clockwise. Also note that the solution may visit more than two plants
located on a same straight line. 

Input

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

Output

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

Sample Input

2
10
1 4 5
2 9 8
3 5 9
4 1 7
5 3 2
6 6 3
7 10 10
8 8 1
9 2 4
10 7 6
14
1 6 11
2 11 9
3 8 7
4 12 8
5 9 20
6 3 2
7 1 6
8 2 13
9 15 1
10 14 17
11 13 19
12 5 18
13 7 3
14 10 16

Sample Output

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

Source

Tehran
1999

大意:

给你n个点 每次从最坐下点往前走  只能想左转弯   问最后最多能走多少个点
并输出顺序

分析:此题考查对于极角排序的理解

极角排序的cmp函数能使得对于 某一个点  把其他的点
按照顺时针或者逆时针的方向进行排序

拿原点为例

则极角排序之后就是顺或逆的点的顺序

拿tan 的a角度比较好理解

手写一下 cmp函数

//首先定义一下cross叉积函数

int cross(point p0, point p1, point p2)//如果是正值
p1在p2的顺时针方向

{

   return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x -
p0.x)*(p1.y - p0.y);

}

int pos;

bool cmp(point p1, point p2)

{

  return cross(point[pos], p1, p2) >
0;//若为>0 则p1在p2的顺时针方向//即按照逆时针排序

}

代码:

 1 #include <iostream>
2 #include <cstring>
3 #include <cstdio>
4 #include <algorithm>
5 using namespace std;
6
7 const int maxn = 55;
8 struct point
9 {
10 int id;
11 int x;
12 int y;
13 }poin[maxn];
14
15 bool cmp(point p1, point p2){
16 if(p1.y != p2.y)
17 return p1.y < p2.y;
18 return p1.x < p2.x;
19 }
20
21 int cross(point p0, point p1, point p2)
22 {
23 return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
24 }
25
26 int Distance(point p1, point p2)
27 {
28 return (p2.x - p1.x)*(p2.x - p1.x) + (p2.y - p1.y)*(p2.y - p1.y);
29 }
30 int pos;
31
32 bool Polar_cmp(point p1, point p2)
33 {
34 int tmp = cross(poin[pos], p1, p2);
35 if(tmp)
36 return tmp > 0;
37 return Distance(poin[pos], p1) <= Distance(poin[pos], p2);
38 }
39
40 int main()
41 {
42 int t, n;
43 scanf("%d",&t);
44 while(t--){
45 scanf("%d", &n);
46 for(int i = 0; i < n; i++)
47 scanf("%d %d %d",&poin[i].id, &poin[i].x, &poin[i].y);
48 int ans[maxn];
49 int k = 0;
50 sort(poin, poin + n, cmp);
51 pos = 0;
52 int cnt = n - 1;
53 for(int i = 1; i <= cnt; i++)
54 {
55 sort(poin + i, poin + n, Polar_cmp);
56 //printf("*%d ",poin[i].id);
57 pos++;
58 }
59 printf("%d",n);
60 for(int i = 0; i < n; i++)
61 printf(" %d",poin[i].id);
62 puts("");
63 }
64 return 0;
65 }

poj1696/hlg1318 蛋疼的蚂蚁,码迷,mamicode.com

时间: 2024-11-04 23:54:51

poj1696/hlg1318 蛋疼的蚂蚁的相关文章

操蛋的EOS,蛋疼的主网“映射”

EOS众筹将在北京时间2018年6月2日06:59:59结束.由于官方之前有明确要求用户进行映射的说法,所以随着EOS主网上线日期越来越近,EOS用户尽早完成"映射"的心情也越来越迫切.但苦于EOS官方"映射"指导的繁琐操作,众多EOS用户对"映射"倍感困难.更有甚者,知道"映射"这个事情,但"根本不知道为何要进行"映射"操作,意义何在? 一.让人蛋疼的EOS"映射" EOS众

多个蚂蚁过独木桥,求通过的最长时间和最短时间

首先对题目进行分析: 求最长时间也就是最后掉下去的一只蚂蚁需要经历的最长时间: 求最短时间也就是最后掉下去的一只蚂蚁需要经历的最短时间:所以让所有的蚂蚁都向距离它们最近的那一段走,这样它们每个走的时间是短的,再求它们中用时最短的那个时间: 分析最长时间如下图所示: /************************************************************************* > File Name: 162.cpp > Author: > Mail:

蚂蚁问题

最近做了几个蚂蚁问题,还蛮有趣的..... 蚂蚁问题第一弹:poj 1852 Ants: Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12214   Accepted: 5366 Description An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a w

最蛋疼的bug:读取图片缩略图(一定要在相册查看下形成缓存)

最近的一个连接服务端的应用,需要读取图片,一般供用户发布商品选择上传图片,初始的图片列表应该是缩略图,只有确定了,才上传原图,OK不多说上代码 package edu.buaa.erhuo; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import

2017辽宁冬令营-4.蚂蚁

蚂蚁(ant.pas/c/cpp)题目大意在平面上有 N 只蚂蚁 N 个树洞,每个树洞只能容纳下一只蚂蚁,蚂蚁们都是直线行走的. 现在蚂蚁们希望能进入树洞中, 希望你能构造出一种方案使得任意两个蚂蚁的路线不相交.保证不会出现三点共线的情况.对于有多种不同的解,输出任意一组即可.无解时,输出-1.输入文件输入文件为 ant.in.第一行为一个数 N.接下来 N 行,每行两个非负整数,第 i+1 行为第 i 号蚂蚁的坐标.接下来 N 行,每行两个非负整数,第 i+N+1 行为第 i 号树洞的坐标.输

[zz]蟑螂蚂蚁蚊子已不住在我家了!这个方法100%见效…

http://mt.sohu.com/20150324/n410238511.shtml 蚂蚁怕酸,蚊子怕辣,蟑螂怕香.在下给各位提供一个不杀生又能驱赶蚂蚁.蚊子.蟑螂的妙法. 一.蚂蚁怕酸味 家里的甜食摆没几分钟,蚂蚁大军立即来袭,让人又气又厌,献给各位一项无毒无污染又安全有效的妙招: 整粒的新鲜柠檬,对切成两半,在看得到蚂蚁的地方及其动线,挤出柠檬汁,并拿着切半有果肉的那一面,沿途涂抹,神奇效果让您再一次验证大自然的奥妙! 二.蚊子怕辣味 蒜头呛退.将蒜头分植于花坛四周,其特殊的辛辣味隐隐飘

懒蚂蚁效应:成功的秘诀在于懒于杂物,勤于思考

概念:懒蚂蚁效应 日本北海道大学进化生物研究小组对一群黑蚂蚁进行了研究.他们把蚂蚁分为三个小组,每组30只,观察他们的行为.研究人员发现,大部分蚂蚁都很勤快地工作,寻找.搬运食物,但是却有少部分蚂蚁好像整天无所事事一样地东张西望. 研究人员在这群蚂蚁身上做上标记,并把这群蚂蚁称为:懒蚂蚁,然后,断绝了整个蚁群的食物来源.有趣的是,这时候那些平时工作很勤快的蚂蚁,突然都不知所措了,而那些"懒蚂蚁"们却挺身而出,带领蚂蚁们向他们侦查到的其它食物转移. 原来,这些"懒蚂蚁"

[OpenJudge 3064]坠落的蚂蚁

试题描述 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如果两只蚂蚁碰头,则它们立即交换速度并继续爬动.三只蚂蚁碰头,则两边的蚂蚁交换速度,中间的蚂蚁仍然静止.如果它们爬到了木棒的边缘(0或100厘米处)则会从木棒上坠落下去.在某一时刻蚂蚁的位置各不相同且均在整数厘米处(即1,2,3,…99厘米),有且只有一只蚂蚁A速度为0,其他蚂蚁均在向左或向右爬动.给出该时刻木棒上的所有蚂蚁位置和初始速度,找出蚂蚁A从此时刻到坠落所需要的时间. 输入

九度oj 题目1159:坠落的蚂蚁

题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如果两只蚂蚁碰头,则它们立即交换速度并继续爬动.三只蚂蚁碰头,则两边的蚂蚁交换速度,中间的蚂蚁仍然静止.如果它们爬到了木棒的边缘(0或100厘米处)则会从木棒上坠落下去.在某一时刻蚂蚁的位置各不相同且均在整数厘米处(即1,2,3,…99厘米),有且只有一只蚂蚁A速度为0,其他蚂蚁均在向左或向右爬动.给出该时刻木棒上的所有蚂蚁位置和初始速度,找出蚂蚁A从此时刻到坠落所需要的时间. 输