有谁作了uva上的361,help me?

galois_godel 2003-05-25 08:23:27
我不是wrong answer ,就是 。。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
#define infinity 1e20
#define EP 1e-10
#define N 10000
//#define double int

struct TPoint{
double x,y;
};
TPoint bp;
struct TLineSeg{
TPoint a,b;
};
int same1(TLineSeg l,TPoint r,TPoint s)
{
double temp;
double dx,dy,dx1,dy1,dx2,dy2;
dx=l.b.x-l.a.x;
dy=l.b.y-l.a.y;
dx1=r.x-l.a.x;
dy1=r.y-l.a.y;
dx2=s.x-l.a.x;
dy2=s.y-l.a.y;
temp=(dx*dy1-dy*dx1)*(dx*dy2-dy*dx2);
return (temp>=0);
}


double max(double a,double b)
{
if(a>b)
return a;
else
return b;
}
double min(double a,double b)
{
if(a<b)
return a;
else
return b;
}
float distance(TPoint p1,TPoint p2)
{
return(sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}


float multiply(TPoint p1,TPoint p2,TPoint p0)
{
return((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
}


int intersect(TLineSeg u,TLineSeg v)
{
return( (max(u.a.x,u.b.x)>=min(v.a.x,v.b.x))&&
(max(v.a.x,v.b.x)>=min(u.a.x,u.b.x))&&
(max(u.a.y,u.b.y)>=min(v.a.y,v.b.y))&&
(max(v.a.y,v.b.y)>=min(u.a.y,u.b.y))&&
(multiply(v.a,u.b,u.a)*multiply(u.b,v.b,u.a)>=0)&&
(multiply(u.a,v.b,v.a)*multiply(v.b,u.b,v.a)>=0));
}

int online(TLineSeg l,TPoint p)
{
return( (multiply(l.b,p,l.a)==0)&&( ((p.x-l.a.x)*(p.x-l.b.x)<0 )||( (p.y-l.a.y)*(p.y-l.b.y)<0 )) );
}


int Euqal_Point(TPoint p1,TPoint p2)
{
return((fabs(p1.x-p2.x)<EP)&&(fabs(p1.y-p2.y)<EP));
}

int intersect_A(TLineSeg u,TLineSeg v)
{
return((intersect(u,v))&&
(!Euqal_Point(u.a,v.a))&&
(!Euqal_Point(u.a,v.b))&&
(!Euqal_Point(u.b,v.a))&&
(!Euqal_Point(u.b,v.b))&&
(!online(u,v.a))&&
(!online(u,v.b))&&
(!online(v,u.a))&&
(!online(v,u.b)));
}


int InsidePolygon(int vcount,TPoint Polygon[],TPoint q)
{
int c=0,i,n;
TLineSeg l1,l2;

l1.a=q;
l1.b=q;
l1.b.x=infinity;
n=vcount;
for (i=0;i<vcount;i++)
{
l2.a=Polygon[i];
l2.b=Polygon[(i+1)%n];
// cout<<i<<" "<<online(l1,Polygon[(i+1)%n])<<" "<<intersect_A(l1,l2)<<endl;
if( Euqal_Point(q,l2.a) ||Euqal_Point(q,l2.b))
return 1;
if(online(l2,q))
return 1;
if((intersect_A(l1,l2))||
(
(online(l1,Polygon[(i+1)%n]))&&
(
(!online(l1,Polygon[(i+2)%n]))&&
(!same1(l1,Polygon[i],Polygon[(i+2)%n]))
||
(online(l1,Polygon[(i+2)%n]))&&
(!same1(l1,Polygon[i],Polygon[(i+3)%n]))
)
)
) c++;

// cout<<c<<endl;
}
//cout<<c<<endl;
return(c%2!=0);
}
void swap(TPoint *a,TPoint *b)
{
TPoint temp=*a;
*a=*b;
*b=temp;
}
/*
int cmp(const void *a,const void *b)
{
TPoint *pa=(TPoint *)a;
TPoint *pb=(TPoint *)b;
double ra=sqrt((pa->x-bx)*(pa->x-bx)+(pa->y-by)*(pa->y-by));
double rb=sqrt((pb->x-bx)*(pb->x-bx)+(pb->y-by)*(pb->y-by));
if(ra<EP)
return 1;
if(rb<EP)
return -1;
double ka=acos((pa->x-bx)/ra);
double kb=acos((pb->x-bx)/rb);
if(fabs(ka-kb)<1e-6)
if(ra>rb)
return 1;
else if(ra<rb)
return -1;
else
return 0;
else if(ka>kb)
return 1;
else
return -1;
//return anglea-angleb;
}*/
int cmp(const void *a,const void *b)
{
double ka,kb;
double da,db;
double ax=((TPoint *)a)->x-bp.x;
double ay=((TPoint *)a)->y-bp.y;
// cout<<" int cmp: "<<bx<<" "<<by<<endl;
double bx=((TPoint *)b)->x-bp.x;
double by=((TPoint *)b)->y-bp.y;

da=sqrt(ax*ax+ay*ay);
db=sqrt(bx*bx+by*by);
ka=acos(ax/da);
kb=acos(bx/db);
if(fabs(ka-kb)<EP)
if(da>db)
return 1;
else if(da<db)
return -1;
else
return 0;
else if(ka>kb)
return 1;
else
return -1;

}
void output(TPoint p[],int len)
{
cout<<"DEBUG IN OUTPUT"<<endl;
int i;
for(i=0;i<len;i++)
{
cout<<p[i].x<<" "<<p[i].y<<endl;
}
cout<<"END OF DEBUG IN OUPUT"<<endl;
}
int convexhull(TPoint p[],int len)
{
int mini=0;
TPoint pp[N];
int pplen;
int i,j;
for(i=1;i<len;i++)
{
if((p[mini].y)>(p[i].y) || ( fabs(p[mini].y-p[i].y)<EP && p[mini].x>p[i].x ))
mini=i;
}
if(mini)
swap(&p[mini],&p[0]);
bp=p[0];
if(len>1)
qsort((void *)(p+1),len-1,sizeof(p[0]),cmp);
//output(p,len);

pp[0]=p[0];
pp[1]=p[1];
pp[2]=p[2];
pplen=3;
j=3;
while(fabs(multiply(pp[0],pp[1],pp[2]))<EP )
{
pp[1]=pp[2];
if(j<len)
pp[2]=p[j++];
else
{
pplen--;
break;
}

}


for(;j<len;j++)
{
// pp[pplen++]=p[j];
// cout<<" for "<<j<<" "<<p[j].x<<" "<<p[j].y<<endl;
while(multiply(p[j],pp[pplen-1],pp[pplen-2])>0 && pplen>2 )
{
// cout<<" multiply(p[j],p[pplen-1],p[pplen-2])="<<multiply(p[j],p[pplen-1],p[pplen-2])<<endl;
pplen--;
// cout<<j<<endl;
}
// cout<<"outwhile multiply(p[j],p[pplen-1],p[pplen-2])="<<multiply(p[j],p[pplen-1],p[pplen-2])<<endl;
pp[pplen++]=p[j];

}
for(i=0;i<pplen;i++)
p[i]=pp[i];
return pplen;
}

TPoint cop[N];
TPoint rob[N];
int c,r,o;
void input()
{
int i;
for(i=0;i<c;i++)
cin>>cop[i].x>>cop[i].y;
if(c>2)
c=convexhull(cop,c);
//cout<<c<<endl;
/// output(cop,c);
for(i=0;i<r;i++)
cin>>rob[i].x>>rob[i].y;
if(r>2)
r=convexhull(rob,r);
}
void slv()
{
int i;
TPoint citizen;
for(i=0;i<o;i++)
{
cin>>citizen.x>>citizen.y;
if(InsidePolygon(c,cop,citizen) )
cout<<" Citizen at ("<<citizen.x<<","<<citizen.y<<") is "<<"safe."<<endl;
else if(InsidePolygon(r,rob,citizen) )
cout<<" Citizen at ("<<citizen.x<<","<<citizen.y<<") is "<<"robbed."<<endl;
else
cout<<" Citizen at ("<<citizen.x<<","<<citizen.y<<") is "<<"neither."<<endl;
}
cout<<endl;
}

int main()
{

int cas=0;
while(cin>>c>>r>>o)
{
if(c==0 && r==0 && o==0)
break;
cout<<"Data set "<<++cas<<":"<<endl;
input();
slv();
}

return 0;
}
...全文
60 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
danger1900 2003-07-02
  • 打赏
  • 举报
回复
告诉我怎么做
mmmcd 2003-06-20
  • 打赏
  • 举报
回复
想结帖,点击次页上方的“管理”...
galois_godel 2003-06-14
  • 打赏
  • 举报
回复
不必回答了。我已过了。这道题目好象有点问题,题目上说数值范围在(-500,500),我照这做,死也不过,后来我把数值改大之后,立即就过了。ft。
浪费了我30多次submission

怎么结帖啊
dawnhorizon 2003-06-07
  • 打赏
  • 举报
回复
翻译一下,应该就会有了:)
galois_godel 2003-06-06
  • 打赏
  • 举报
回复
没人知道吗
galois_godel 2003-05-28
  • 打赏
  • 举报
回复
题目如下:
Cops and Robbers

You are to simulate a game of Cops and Robbers. In this game, cops, robbers, and other citizens are represented as points in a two-dimensional plane. A citizen is said to be safe if it is within a triangle formed by three cops. A citizen is said to be robbed if it is not safe and is within a triangle formed by three robbers. A citizen is neither safe nor robbed if it satisfies neither of the above conditions. For purposes of this problem, a triangle consists of three non-collinear points, and a point is within a triangle if it is inside or on the boundary of the triangle.


In the following diagram, filled circles represent cops, filled squares represent robbers, and filled triangles represent citizens. Dashed lines indicate triangles formeby cops or robbers



In this example, citizens A and B are safe, citizen C is robbed, and citizen D is neither.


Given a set of cops and robbers and several citizen queries, efficiently determine whether each citizen is safe, robbed, or neither.


Input
The input consists of several data sets. The first line of each data set contains three non-negative integers c, r, and o: the number of cops, robbers, and other citizens, respectively. c, r, and o will each be at most 200. The next c lines contain the (x, y) coordinates of each cop, one per line. The next r lines contain the (x, y) coordinates of each robber, one per line. The next o linescontain the (x, y) coordinates of each other citizen, one per line. All coordinates are integers between -500 and 500 inclusive.


Your program must stop processing input when it encounters adata set in which c, r, and o are all zero.


Output
Output for each data set begins with a line identifying the data set. For each other citizen in the data set, output the line



Citizen at (x,y) is status.



where (x,y) is the location of the citizen from the input and status is one of safe, robbed or neither. Follow the format given in the Sample Output. Leave a blank line after the output from each data set.


Sample Input

3 3 2
0 0
10 0
0 10
20 20
20 0
0 20
5 5
15 15
3 3 1
0 0
10 0
0 10
20 20
20 0
0 20
40 40
0 0 0

Sample Output

Data set 1:
Citizen at (5,5) is safe.
Citizen at (15,15) is robbed.

Data set 2:
Citizen at (40,40) is neither.
LeeMaRS 2003-05-25
  • 打赏
  • 举报
回复
...你帖个题目过来, 会有更多的人帮你看..

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧