又一次用链表AC题目,激动啊。。看来链表也不是那么难懂,但是用时要小心,分清楚p是谁,p->next是谁,这就像数组中的i,j等,明白了是谁的指向,链表当然也就不怎么难懂了。。加油。。\(^o^)/~
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#define eps 1e-8
using namespace std;
int n;
struct PP
{
double x,y;
};
struct Point
{
double x1,x2,y1,y2;
int num;//记录的是第几个木条
};
Point tem;
typedef struct Node
{
Point data;
struct Node * next;
}Node,*LinkList;
void InitList(LinkList *L)
{
*L = (LinkList)malloc(sizeof(Node));
(*L)->next = NULL;
}
double multi(PP p1,PP p2,PP p0)
{
return((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
}
bool fun(Point temp1,Point temp2)
{
PP s1,e1,s2,e2;
s1.x = temp1.x1;
s1.y = temp1.y1;
e1.x = temp1.x2;
e1.y = temp1.y2;
s2.x = temp2.x1;
s2.y = temp2.y1;
e2.x = temp2.x2;
e2.y = temp2.y2;
bool k = (max(s1.x,e1.x)>= min(s2.x,e2.x))&&
(max(s2.x,e2.x)>= min(s1.x,e1.x))&&
(max(s1.y,e1.y)>= min(s2.y,e2.y))&&
(max(s2.y,e2.y)>= min(s1.y,e1.y))&&
(multi(s2,e1,s1) * multi(e1,e2,s1) > -eps)&&
(multi(s1,e2,s2) * multi(e2,e1,s2) > -eps);
return k;
}
void print(LinkList *L)
{
printf("Top sticks:");
Node *p;
p = (*L)->next;
while(p!=NULL)
{
printf(" %d",(p->data).num);
p = p->next;
if(p!=NULL)
printf(",");
}
printf(".\n");
}
void rnew(LinkList *L,Point tem)
{
Node *p,*pp,*r;
pp = *L;
p = (*L)->next;
while(p->next!=NULL)//不和自己判断相交
{
if(!fun(p->data,tem))
{//不相交,遍历下一个节点。
pp = p;
p = p->next;
}
else
{//相交,删去该节点。
pp->next = p->next;
p = p->next;
}
}
}
void CreateFromTail(LinkList *L)
{
Node *r,*s;
r = *L;
for(int i = 1;i <= n;i++)
{
scanf("%lf%lf%lf%lf",&tem.x1,&tem.y1,&tem.x2,&tem.y2);
tem.num = i;
s = (Node *)malloc(sizeof(Node));
s->data = tem;
r->next = s;
r = s;
r->next = NULL;
rnew(L,tem);
}
}
int main()
{
LinkList Head;
while(scanf("%d",&n)!=EOF)
{
if(!n) break;
InitList(&Head);
CreateFromTail(&Head);
print(&Head);
}
return 0;
}