Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

哪位大神帮我

Posted by qiuhecheng at 2013-04-18 17:18:55 on Problem 1039
#include<iostream>
#include<cmath>
using namespace std;
double d[22];
typedef struct point 
{
	double x,y;
	point(double x=0,double y=0):x(x),y(y){}
}vector;
point p[22];
double cross(vector a,vector b)
{
	return a.x*b.y-a.y*b.x;
}
double f(int a,double s,point i,point j)
{
	vector v(j.x-i.x,j.y-i.y),w(1,s);
	vector u(i.x-p[a].x,i.y-p[a].y-d[a]);
	double t=cross(v,w);
	if(!t) 
		return 0;
	t=cross(w,u)/t;
	if(t>=0 && t<=1) 
		return i.x+v.x*t;
	return 0;
}
int f1(point a,point b,point c)
{
	vector v1(b.x-a.x,b.y-a.y),v2(c.x-b.x,c.y-b.y);
	double t=cross(v1,v2);
	if(t>0) 
		return 1;
	return 0;
}
void f2(int i,int j,double m,double &right)
{
	double ang=0;
	if(j!=i+1 || d[i])
	{
		ang=f(i,m,p[j-1],p[j]);
		if(ang>right) right=ang;
	}
	if(!ang && (j!=i+1 || !d[i]))
	{
		ang=f(i,m,point(p[j-1].x,p[j-1].y-1),point(p[j].x,p[j].y-1));
		if(ang>right) right=ang;
	}
}
int main()
{
	int i,j,n;
	double ang,ang1,ang2,max,min,right;
	while(cin>>n && n)
	{
		for(i=0;i<n;i++)
			cin>>p[i].x>>p[i].y;
		for(i=1;i<n-1;i++)
		{
			if(f1(p[i-1],p[i],p[i+1])) d[i]=0;
			else d[i]=-1;
		}
		if(p[n-1].y<p[n-2].y) d[n-1]=0;
		else d[n-1]=-1;
		right=p[0].x;
		for(i=1;i<n;i++)
		{
			max=(p[i].y+d[i]-p[0].y)/(p[i].x-p[0].x);
			min=(p[i].y+d[i]-p[0].y+1)/(p[i].x-p[0].x);
			for(j=1;j<i;j++)
			{
				ang=(p[i].y+d[i]-p[j].y-d[j])/(p[i].x-p[j].x);
				if(d[j])
				{
					if(min>ang) min=ang;
				}
				else if(max<ang) max=ang;
				if(max>min) break;
			}
			if(max<=min)
			{
				if(i==n-1) 
				{
					right=p[i].x;break;
				}
				ang1=ang2=1;
				for(j=i+1;j<n;j++)
				{
					if(ang1)
					{
						ang1=f(i,max,p[j],point(p[j].x,p[j].y-1));
						if(!ang1)
							f2(i,j,max,right);
					}
					if(ang2)
					{
						ang2=f(i,min,p[j],point(p[j].x,p[j].y-1));
						if(!ang2)
							f2(i,j,min,right);
					}
					if(!ang1 && !ang2) break;
				}
				if(j==n-1 && (ang1 || ang2))
				{
					right=p[j].x;break;
				}
			}
		}
		if(right==p[n-1].x) printf("Through all the pipe.\n");
		else printf("%.2lf\n",right);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator