poj 2074 Line of Sight (计算几何,细节题)

Line of Sight
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3935   Accepted: 1229


An architect is very proud of his new home and wants to be sure it can be seen by people passing by his property line along the street. The property contains various trees, shrubs, hedges, and other obstructions that may block the view. For the purpose of this problem, model the house, property line, and obstructions as straight lines parallel to the x axis: 

To satisfy the architect’s need to know how visible the house is, you must write a program that accepts as input the locations of the house, property line, and surrounding obstructions and calculates the longest continuous portion of the property line from which the entire house can be seen, with no part blocked by any obstruction.


Because each object is a line, it is represented in the input file with a left and right x coordinate followed by a single y coordinate: 
< x1 > < x2 > < y > 
Where x1, x2, and y are non-negative real numbers. x1 < x2 
An input file can describe the architecture and landscape of multiple houses. For each house, the first line will have the coordinates of the house. The second line will contain the coordinates of the property line. The third line will have a single integer that represents the number of obstructions, and the following lines will have the coordinates of the obstructions, one per line. 
Following the final house, a line “0 0 0” will end the file. 
For each house, the house will be above the property line (house y > property line y). No obstruction will overlap with the house or property line, e.g. if obstacle y = house y, you are guaranteed the entire range obstacle[x1, x2] does not intersect with house[x1, x2].


For each house, your program should print a line containing the length of the longest continuous segment of the property line from which the entire house can be to a precision of 2 decimal places. If there is no section of the property line where the entire house can be seen, print “No View”.

Sample Input

Sample Output


然后从左往右扫,求得在li上的两点p1,p2 分别使得 p1.x  li[i].x2 house.x1 和 p2.x li[i+1].x1 house.x2三点共线。
  1. 只说了房子一定在line上面,而障碍物是可能不在房子和线之间的。如果障碍物在房子以上或者线以下就跳过(包括相等)
  2. 第一个点和最后一个点要特殊处理。因为第一个的p1左边到li.x1和 第二个点的p2右边到li.x2都是有解的范围。
  3. 可能没有障碍物(n==0),此时答案为li.x2-li.x1
  4. 可能有障碍物,但是障碍物没起作用,就是说障碍物没有挡住任何视线,此时答案同上。所以3,4点可以和在一起考虑。可以用一个boolean变量beblocked记录是否有被遮挡的情况。如果最后发现没有遮挡的情况,那么答案为li.x2-li.x1;
  5. 求得到的p1,p2点可能不在line上,要注意维护下边界。
  6. 以上的情况,对于第二点中提及的特殊处理中。。也不要漏写。

View Code


Posted in ACM