题目描述
PDF
输入格式
输出格式
题意翻译
题目描述
考古学家考察金字塔。金字塔的底层是由一系列直线墙构成的,这些直线墙相交形成许多封闭的房间。目前,没有门存在以允许进入任何房间。这项技术也精确定位了宝藏室的位置。
考古学家想通过炸门进入宝藏室。他们想炸门的数量最少。只能在墙壁的中点对门进行爆破。求炸门的最小数目。
下图是一个例子。
image.png
输入格式
有多组输入。输入的第一行是数据组数。每组输入的第一行是一个整数 nn ,指定内墙的数量,以下 nn 行是墙的整数端点x_1x
1
y_1y
1
x_2x
2
y_2y
2
。金字塔的4个围墙在 (0,0)(0,0),(0,100)(0,100),(100,100)(100,100),(100,0)(100,0) 处有固定的端点,不在输入的围墙列表中。内墙总是从一个外墙跨越到另一个外墙,没有三个及以上堵墙交于一点。你可以假设没有两堵墙重合。内墙挂牌后,最后一行将包含宝藏室中宝藏的浮点坐标(保证不与墙重合)。
每组输入之间用空行隔开。
输出格式
对于每种情况,输出一行,即需要炸门的最小数量,格式见样例。
每两组输出之间用空行隔开。
数据范围
0\le n\le 300≤n≤30
输入输出样例
输入 #1复制
1
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
输出 #1复制
Number of doors = 2
题意看题中的图就行:问你从给定的点出发最少需要穿过几条线段才能从正方形中出去(边界也算)。
因为nn很小,可以考虑比较暴力的做法。枚举在边界中的哪一个点离开的。也就是枚举四周的点(x, y)(x,y),并和起点(x_0, y_0)(x
0
,y
0
)连成线段,求和多少条线段相交。
但是因为点可以是实数,所以不知道怎么枚举。不过想想就知道,同一个区间中的点是等价的。因此我们只要枚举线段的端点即可。
至于判断线段相交,用叉积实现:对于线段ABAB和CDCD,如果(\overrightarrow{AB} \times \overrightarrow{AC}) * (\overrightarrow{AB} \times \overrightarrow{AD}) < 0(
AB
×
AC
)∗(
AB
×
AD
)<0且(\overrightarrow{CD} \times \overrightarrow{CA}) * (\overrightarrow{CD} \times \overrightarrow{CB}) < 0(
CD
×
CA
)∗(
CD
×
CB
)<0,则线段ABAB,CDCD相交。
(别忘了多组数据和n = 0n=0的情况,且每组输出之间有空行……)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 50;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n;
struct Vec
{
db x, y;
db operator * (const Vec& oth)const
{
return x * oth.y - oth.x * y;
}
};
struct Point
{
db x, y;
Vec operator - (const Point& oth)const
{
return (Vec){x - oth.x, y - oth.y};
}
}a[maxn], b[maxn], P;
int solve(Point A, Point B)
{
Vec AB = B - A;
int ret = 0;
for(int i = 1; i <= n; ++i)
{
Vec AC = a[i] - A, AD = b[i] - A;
Vec CD = b[i] - a[i], CB = B - a[i];
if((AB * AC) * (AB * AD) < -eps && (CD * AC) * (CD * CB) > eps) ret++;
}
return ret;
}
int main()
{
int T = read();
while(T--)
{
int ans = INF;
n = read();
for(int i = 1; i <= n; ++i) a[i].x = read(), a[i].y = read(), b[i].x = read(), b[i].y = read();
scanf("%lf%lf", &P.x, &P.y);
for(int i = 1; i <= n; ++i)
{
ans = min(ans, solve(a[i], P));
ans = min(ans, solve(b[i], P));
}
if(!n) ans = 0;
printf("Number of doors = ");
write(ans + 1), enter;
if(T) enter;
}
return 0;
}