# Poj Solution 1066

```http://poj.org/problem?id=1066

#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAX 35
int N;
using namespace std;
typedef struct Point
{
double x,y;
Point() {}
Point(double tx,double ty):x(tx),y(ty) {}
}Vector;

int WallY0[MAX]; //��ַ����ݸ���
int WallY100[MAX];
int WallX0[MAX];
int WallX100[MAX];
struct Line
{
Point A,B;

Line()  {}
Line(Point &tA,Point &tB):A(tA),B(tB)
{
if (A.x > B.x)
{
Point t = A;
A = B;
B = t;
}
}
};

Line T[MAX];
Point S;

double muli(Vector A,Vector B)
{
return A.x * B.y - A.y * B.x;
}

Vector GetVector(Point A,Point B)
{
Vector t;
t.x = B.x - A.x;
t.y = B.y - A.y;
return t;
}

bool IsCross(Line L1,Line L2)
{
Vector A = GetVector(L1.A,L1.B);
Vector A1 = GetVector(L1.A,L2.A);
Vector A2 = GetVector(L1.A,L2.B);

Vector B = GetVector(L2.A,L2.B);
Vector B1 = GetVector(L2.A,L1.A);
Vector B2 = GetVector(L2.A,L1.B);

double Ans_A1 = muli(A,A1);
double Ans_A2 = muli(A,A2);
if ((Ans_A1 > 0 && Ans_A2 > 0) || (Ans_A1 < 0 && Ans_A2 < 0))
return false;
if (Ans_A1 == 0 && Ans_A2 == 0)
{
Line t1 = L1.A.x < L2.A.x ? L1 : L2;
Line t2 = L1.A.x < L2.A.x ? L2 : L1;
if (t2.A.x > t1.B.x)
return false;
}

double Ans_B1 = muli(B,B1);
double Ans_B2 = muli(B,B2);
if ((Ans_B1 > 0 && Ans_B2 > 0) || (Ans_B1 < 0 && Ans_B2 < 0))
return false;
if (Ans_B1 == 0 && Ans_B2 == 0)
{
Line t1 = L1.A.x < L2.A.x ? L1 : L2;
Line t2 = L1.A.x < L2.A.x ? L2 : L1;
if (t2.A.x > t1.B.x)
return false;
}
return true;
}

void PreProcess()
{
WallX0[0] = 1;
WallX0[1] = 0;
WallX100[0] = 1;
WallX100[1] = 0;
WallY0[0] = 1;
WallY0[1] = 0;
WallY100[0] = 1;
WallY100[1] = 0;
for (int i = 0;i < N;i++)
{
if (T[i].A.x == 0)
WallX0[++WallX0[0]] = T[i].A.y;
if (T[i].B.x == 0)
WallX0[++WallX0[0]] = T[i].B.y;
if (T[i].A.x == 100)
WallX100[++WallX100[0]] = T[i].A.y;
if (T[i].B.x == 100)
WallX100[++WallX100[0]] = T[i].B.y;
if (T[i].A.y == 0)
WallY0[++WallY0[0]] = T[i].A.x;
if (T[i].B.y == 0)
WallY0[++WallY0[0]] = T[i].B.x;
if (T[i].A.y == 100)
WallY100[++WallY100[0]] = T[i].A.x;
if (T[i].B.y == 100)
WallY100[++WallY100[0]] = T[i].B.x;
}

sort(WallX0 + 1,WallX0 + WallX0[0] + 1);
sort(WallX100 + 1,WallX100 + WallX100[0] + 1);
sort(WallY0 + 1,WallY0 + WallY0[0] + 1);
sort(WallY100 + 1,WallY100 + WallY100[0] + 1);

if(WallX0[WallX0[0]] != 100)
WallX0[++WallX0[0]] = 100;
if(WallX100[WallX100[0]] != 100)
WallX100[++WallX100[0]] = 100;
if(WallY0[WallY0[0]] != 100)
WallY0[++WallY0[0]] = 100;
if(WallY100[WallY100[0]] != 100)
WallY100[++WallY100[0]] = 100;
// for(int i = 1;i < WallY0[0];i++)
//{
//     cout<<WallY0[i]<<endl;
//}
}

{
PreProcess();
int Count = 0x7FFFFFFF;
int i = 1;
while(WallX0[i] == 0) i++;
for (;i <= WallX0[0];i++)
{
double mid = (WallX0[i] + WallX0[i - 1]) / 2.0;
Line l;
l.A = S;
l.B = Point(0,mid);

int c = 0;
for (int j = 0;j < N;j++)
{
if (IsCross(T[j],l))
{
c++;
//cout<<WallX0[i - 1]<<' '<<WallX0[i]<<" Cross "<<T[j].A.x<<' '<<T[j].A.y<<' '<<T[j].B.x<<' '<<T[j].B.y<<endl;
}
}
if (c < Count)
{
Count = c;
//cout<<0<<' '<<WallX0[i]<<endl;
}
}
i = 1;
while(WallX100[i] == 0) i++;
for (;i <= WallX100[0];i++)
{
double mid = (WallX100[i] + WallX100[i - 1]) / 2.0;
Line l;
l.A = S;
l.B = Point(100,mid);

int c = 0;
for (int j = 0;j < N;j++)
{
if (IsCross(T[j],l))
{
c++;
//cout<<WallX100[i -1]<<' '<<WallX100[i]<<" Cross "<<T[j].A.x<<' '<<T[j].A.y<<' '<<T[j].B.x<<' '<<T[j].B.y<<endl;
}
}
if (c < Count)
{
Count = c;
//cout<<100<<' '<<WallX100[i]<<endl;
}
}

i = 1;
while(WallY0[i] == 0) i++;
for (;i <= WallY0[0];i++)
{
double mid = (WallY0[i] + WallY0[i - 1]) / 2.0;
Line l;
l.A = S;
l.B = Point(mid,0);

int c = 0;
for (int j = 0;j < N;j++)
{
if (IsCross(T[j],l))
{
c++;
//cout<<WallY0[i - 1]<<' '<<WallY0[i]<<" Cross "<<T[j].A.x<<' '<<T[j].A.y<<' '<<T[j].B.x<<' '<<T[j].B.y<<endl;
}
}
if (c < Count)
{
Count = c;
//cout<<WallY0[i]<<' '<<0<<endl;
}
}

i = 1;
while(WallY100[i] == 0) i++;
for (;i <= WallY100[0];i++)
{
double mid = (WallY100[i] + WallY100[i - 1]) / 2.0;
Line l;
l.A = S;
l.B = Point(mid,100);

int c = 0;
for (int j = 0;j < N;j++)
{
if (IsCross(T[j],l))
{
c++;
//cout<<WallY100[i-1]<<' '<<WallY100[i]<<" Cross "<<T[j].A.x<<' '<<T[j].A.y<<' '<<T[j].B.x<<' '<<T[j].B.y<<endl;
}
}
if (c < Count)
{
Count = c;
//cout<<WallY100[i]<<' '<<100<<endl;
}
}
cout<<"Number of doors = "<<Count+1<<endl;
}

int main()
{
while (cin>>N)
{
for (int i = 0;i < N;i++)
{
scanf("%lf%lf%lf%lf",&T[i].A.x,&T[i].A.y,&T[i].B.x,&T[i].B.y);
}
scanf("%lf%lf",&S.x,&S.y);
}
return 0;
}

/*
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.
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
*/

```
This entry was posted in poj. Bookmark the permalink.