poj1066
很好的一道题。题意是,一个正方形围墙内有一些交错的内墙,内墙的端点都在正方形上,在正方形内部有一个点,求从正方形外到这个点的最少要走的门数,门只能是线段的中点。
思路很巧妙,因为从一个点到终点不可能“绕过”围墙,只能传过去,所以门是否开在中点是无所谓的,只要求四周线段中点到终点的线段与墙的最少交点个数即可。更进一步,实际上,只需判断四周围墙的所有点与终点的连线与内墙的最少交点加一即可。
请看下图的红色线,与蓝色线交点,即是上述的交点。
#include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)>0?(x):-(x))<eps) #define pi acos(-1.0) struct point { double x, y; }; struct line { point a, b; }; //计算cross product (P1-P0)x(P2-P0) double xmult(point p1, point p2, point p0) { return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y); } //计算dot product (P1-P0).(P2-P0) double dmult(point p1, point p2, point p0) { return (p1.x - p0.x)*(p2.x - p0.x) + (p1.y - p0.y)*(p2.y - p0.y); } //两点距离 double distance(point p1, point p2) { return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y)); } //判三点共线 bool dots_inline(point p1, point p2, point p3) { return zero(xmult(p1, p2, p3)); } //判点是否在线段上,包括端点 bool dot_online_in(point p, line l) { return zero(xmult(p, l.a, l.b)) && (l.a.x - p.x)*(l.b.x - p.x) < eps && (l.a.y - p.y)*(l.b.y - p.y) < eps; } //判点是否在线段上,不包括端点 bool dot_online_ex(point p, line l) { return dot_online_in(p, l) && (!zero(p.x - l.a.x) || !zero(p.y - l.a.y)) && (!zero(p.x - l.b.x) || !zero(p.y - l.b.y)); } //判两点在线段同侧,点在线段上返回0 bool same_side(point p1, point p2, line l) { return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) > eps; } //判两点在线段异侧,点在线段上返回0 bool opposite_side(point p1, point p2, line l) { return xmult(l.a, p1, l.b)*xmult(l.a, p2, l.b) < -eps; } //判两线段相交,包括端点和部分重合 bool intersect_in(line u, line v) { if (!dots_inline(u.a, u.b, v.a) || !dots_inline(u.a, u.b, v.b)) return !same_side(u.a, u.b, v) && !same_side(v.a, v.b, u); return dot_online_in(u.a, v) || dot_online_in(u.b, v) || dot_online_in(v.a, u) || dot_online_in(v.b, u); } //判两线段相交,不包括端点和部分重合 bool intersect_ex(line u, line v) { return opposite_side(u.a, u.b, v) && opposite_side(v.a, v.b, u); } int main() { point p[100]; line wall[35], link[100]; int n; while (std::cin >> n) { int j = 0; for (int i = 0; i < n << 1; i++)//边界点 { std::cin >> p[i].x >> p[i].y; } for (int i = 0; i < n << 1; i++)//构造墙 { wall[j].a = p[i]; wall[j++].b = p[++i]; } double x, y; std::cin >> x >> y; int k = 0; for (int i = 0; i < n << 1; i++)//构造宝藏点到边界所有点的连线 { link[k].a = p[i]; link[k].b.x = x, link[k++].b.y = y; } //for (int i = 0; i < n; i++) //{ // std::cout << wall[i].a.x << ' ' << wall[i].a.y << ' ' << wall[i].b.x << ' ' << wall[i].b.y << std::endl; //} //for (int i = 0; i < n << 1; i++) //{ // std::cout << link[i].a.x << ' ' << link[i].a.y << ' ' << link[i].b.x << ' ' << link[i].b.y << std::endl; //} int min = 100000; for (int i = 0; i < n << 1; i++) { int count = 0; for (int j = 0; j < n; j++) { if (intersect_ex(link[i], wall[j])) { count++; /*std::cout << link[i].a.x << '%' << link[i].a.y << '%' << link[i].b.x << '%' << link[i].b.y << std::endl; std::cout << wall[j].a.x << '%' << wall[j].a.y << '%' << wall[j].b.x << '%' << wall[j].b.y << std::endl << std::endl;*/ } } //std::cout << count << std::endl; if (count < min) min = count; } if (n == 0) std::cout << "Number of doors = 1" << std::endl; else std::cout <<"Number of doors = "<< min + 1<< std::endl; } }