Treasure Hunt
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5320 | Accepted: 2192 |
Description
Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine that the lower floor of the pyramid is constructed from a series of straightline walls, which intersect to form numerous enclosed chambers. Currently, no doors exist to allow access to any chamber. This state-of-the-art technology has also pinpointed the location of the treasure room. What these dedicated (and greedy) archeologists want to do is blast doors through the walls to get to the treasure room. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for dynamite) they want to blast through the minimum number of doors. For structural integrity purposes, doors should only be blasted at the midpoint of the wall of the room being entered. You are to write a program which determines this minimum number of doors.
An example is shown below:
An example is shown below:
![](https://i-blog.csdnimg.cn/blog_migrate/00633d3443fee9be8a0e40bc2b640508.jpeg)
Input
The input will consist of one case. The first line will be an integer n (0 <= n <= 30) specifying number of interior walls, followed by n lines containing integer endpoints of each wall x1 y1 x2 y2 . The 4 enclosing walls of the pyramid have fixed endpoints at (0,0); (0,100); (100,100) and (100,0) and are not included in the list of walls. The interior walls always span from one exterior wall to another exterior wall and are arranged such that no more than two walls intersect at any point. You may assume that no two given walls coincide. After the listing of the interior walls there will be one final line containing the floating point coordinates of the treasure in the treasure room (guaranteed not to lie on a wall).
Output
Print a single line listing the minimum number of doors which need to be created, in the format shown below.
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 这道题乍一看觉得很难,但是看一会就笑了,再看一会就又开始纠结了。如果从起点开始走向目标,经过了两条直线的交点怎么办,在这里是不用考虑那么多的,只需从目标点向给出的点 枚举即可,然后和给出的线段枚举,比较,找出最小的情况。 bug: 考虑N=0 的情况,输出时是 printf("Number of doors = 1\n"); 我的输出是 printf("Number of doors = %d\n",((min1<min2)?min1:min2)+1); 一开始总是找不出错误,最后才发现我的原始输出是printf("Number of doors = %d\n",(min1<min2)?min1:min2+1); 没有加括号,卡了好长时间。。。。。失误了#include <stdio.h> #include <math.h> struct point { double x; double y; }o; struct line { struct point a; struct point b; }l[40]; int sig(double k) { if(fabs(k)<1e-8) return 0; else return (k>0)?1:-1; } int mul(struct point p1,struct point p2,struct point p3) { return sig((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y)); } int main (void) { //freopen("1066.txt","r",stdin); int t,i,j,k,sum,min1,min2; while(scanf("%d",&t)!=EOF) { if(t==0) { scanf("%lf%lf",&o.x,&o.y); printf("Number of doors = 1\n"); continue; } k=0; min1=999; min2=999; for(i=1;i<=t;i++) { scanf("%lf%lf%lf%lf",&l[i].a.x,&l[i].a.y,&l[i].b.x,&l[i].b.y); } scanf("%lf%lf",&o.x,&o.y); //枚举点与四周各点的连线(外循环),枚举每条线段(内循环)有交点加一 for(i=1;i<=t;i++) { sum=0; for(j=1;j<=t;j++) { // if(i==j) // continue; if(mul(o,l[i].a,l[j].a)*mul(o,l[i].a,l[j].b)<0&&mul(l[j].a,l[j].b,o)*mul(l[j].a,l[j].b,l[i].a)<0) sum++; } min1=(min1>sum)?sum:min1; } for(i=1;i<=t;i++) { sum=0; for(j=1;j<=t;j++) { // if(i==j) // continue; if(mul(o,l[i].b,l[j].a)*mul(o,l[i].b,l[j].b)<0&&mul(l[j].a,l[j].b,o)*mul(l[j].a,l[j].b,l[i].b)<0) sum++; } min2=(min2>sum)?sum:min2; } printf("Number of doors = %d\n",((min1<min2)?min1:min2)+1); } return 0; }