题目链接:传送门
Treasure Hunt
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7164 | Accepted: 2938 |
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
解题思路:枚举每道墙的两个端点与终点构成的线段,严格穿过墙的数目中的最小值,当n=0时结果为1(当时把这个判断放在了第一行,结果一直wa,后来在维哥的帮助下才发现数据还没输完)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 100;
const int M = 1000;
const int INF = 0x3fffffff;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x; y = _y;
}
Point operator - (const Point&b)const{
return Point(x-b.x,y-b.y);
}
//叉积
double operator ^ (const Point&b)const{
return x*b.y - y*b.x;
}
//点积
double operator * (const Point&b)const{
return x*b.x + y*b.y;
}
void input(){
scanf("%lf%lf",&x,&y);
}
};
struct Line
{
Point s,e; //s点为位置高的点
Line(){}
Line(Point _s,Point _e){
if( _s.y < _e.y ) swap(_s,_e);
s = _s; e = _e;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为2是相交
//只有第一个值是2时,交点才有意义
pair<int,Point>operator & (const Line&b)const{
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == 0){
if(sgn((s-b.e)^(b.s-b.e)) == 0) return make_pair(0,res);
else return make_pair(1,res);
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x-s.x)*t;
res.y += (e.y-s.y)*t;
return make_pair(2,res);
}
};
//判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x)&&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x)&&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y)&&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y)&&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) < 0 &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) < 0;
}
Point point[80];
Line line[40];
int main()
{
int n;
while( ~scanf("%d",&n) ){
Point p1,p2,ps;
int tot = 0;
for( int i = 0 ; i < n ; ++i ){
p1.input();
p2.input();
point[tot++] = p1;
point[tot++] = p2;
line[i] = Line(p1,p2);
}
ps.input();
int re,ans = INF;
for( int i = 0 ; i < tot ; ++i ){
re = 0;
for( int j = 0 ; j < n ; ++j ){
if( inter(line[j],Line(ps,point[i])) ) re++;
}
ans = min(ans,re);
}
if( n == 0 ) printf("Number of doors = 1\n");
else printf("Number of doors = %d\n",ans+1);
}
return 0;
}