标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6328 | Accepted: 2627 |
Description
Input
Output
Sample Input
7 20 0 37 100 40 0 76 100 85 0 0 75 100 90 0 90 0 71 100 61 0 14 100 38 100 47 47 100 54.5 55.4
Sample Output
Number of doors = 2
题意:求从矩形上到宝藏点需要破开的最少的门。。相交点算两张门。
题解:本人方法是,,直接全部枚举,碰到和矩形边相交的直线直接跳过。。最后记得+1
///判断直线与线段相交 ///做法:枚举每两个端点,要是存在一条直线经过这两个端点并且和所有线段相交就OK,但是不能为重合点. #include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int N = 50; const double eps = 1e-8; struct Point { double x,y; }; struct Line { Point a,b; } line[N]; int n; double cross(Point a,Point b,Point c){ return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); } bool isCross(Point a, Point b, Point c, Point d) { if (cross(c, b, a)*cross(b, d, a)<0)return false; if (cross(a, d, c)*cross(d, b, c)<0)return false; return true; } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y); } Point e; scanf("%lf%lf",&e.x,&e.y); int mi = 99999999; int cnt; for(int j=1; j<=4; j++) { for(int i=0; i<=100; i++) { Point s; if(j==1) s.x=i,s.y=0; if(j==2) s.x=0,s.y=i; if(j==3) s.x=100,s.y = i; if(j==4) s.x=i,s.y=100; cnt=0; for(int k=0; k<n; k++){ if(fabs(s.x-line[k].a.x)<eps&&fabs(s.y-line[k].a.y)<eps) continue; if(fabs(s.x-line[k].b.x)<eps&&fabs(s.y-line[k].b.y)<eps) continue; if(isCross(s,e,line[k].a,line[k].b)){ cnt++; } } //printf("%d\n",cnt); if(mi>cnt) mi = cnt; } } if(n==0) printf("Number of doors = 1\n"); else printf("Number of doors = %d\n",mi+1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5448244.html