Irv Kenneth Diggit works for a company that excavates trenches, digs holes and generally tears up people's yards. Irv's job is to make sure that no underground pipe or cable is underneath where excavation is planned. He has several different maps, one for each utility company, showing where their conduits lie, and he needs to draw one large, consolidated map combining them all. One approach would be to simply draw each of the smaller maps one at a time onto the large map. However, this often wastes time, not to mention ink for the pen-plotter in the office, since in many cases portions of the conduits overlap with each other (albeit at different depths underground). What Irv wants is a way to determine the minimum number of line segments to draw given all the line segments from the separate maps. node
Input will consist of multiple input sets. Each set will start with a single line containing a positive integer n indicating the total number of line segments from all the smaller maps. Each of the next n lines will contain a description of one segment in the format x1 y1 x2 y2 where (x1,y1) are the coordinates of one endpoint and (x2,y2) are the coordinates of the other. Coordi- nate values are floating point values in the range 0... 1000 specified to at most two decimal places. The maximum number of line segments will be 10000 and all segments will have non-zero length. Following the last input set there will be a line containing a 0 indicating end of input; it should not be processed. ios
For each input set, output on a single line the minimum number of line segments that need to be drawn on the larger, consolidated map. git
3 1.0 10.0 3.0 14.0 0.0 0.0 20.0 20.0 10.0 28.0 2.0 12.0 2 0.0 0.0 1.0 1.0 1.0 1.0 2.15 2.15 2 0.0 0.0 1.0 1.0 1.0 1.0 2.15 2.16 0
2 1 2
本題比較水,主要要注意浮點數的相等判斷處理,以及輸入數據存儲順序的整理,而後排序統計便可。特別注意的是,本題的數據是線段,不是直線,因此可否合併要判斷兩條共線線段的相鄰端點;還有點斜式沒法表示垂直於x軸的直線,這裏不妨設它斜率爲正無窮(賦個很大的數便可),同時端點判斷也要特殊處理。 app
// Problem#: 1004 // Submission#: 1792749 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <iostream> #include <algorithm> #include <cmath> using namespace std; #define MAX 10000 #define INF 1000000 #define ESP 1e-6 struct node{ double x1,y1,x2,y2; double k,b; }buffer[MAX]; inline bool dcmp( double a, double b ){ return ( fabs(a-b)<=ESP ); } inline double max( double a, double b ){ return a>b?a:b; } inline void swap( double &a, double &b ){ double t = a; a = b; b = t; } inline bool cmp( node a, node b ){ if( !dcmp(a.k,b.k) ) return a.k<b.k; if( !dcmp(a.b,b.b) ) return a.b<b.b; if( !dcmp(a.x1,b.x1) ) return a.x1<b.x1; if( !dcmp(a.y1,b.y1) ) return a.y1<b.y1; if( !dcmp(a.x2,b.x2) ) return a.x2<b.x2; return a.y2<b.y2; } inline bool judge( node a, node b ){ if( dcmp(a.k,b.k) && dcmp(a.b,b.b) ){ if( dcmp(a.k,INF) ) return dcmp(b.y1,a.y2) || b.y1<a.y2; else return dcmp(a.x2,b.x1) || a.x2>b.x1; } return false; } int main(){ int n,re; while( cin>>n && n ){ re = n; for( int i=0 ; i<n ; i++ ){ cin >> buffer[i].x1 >> buffer[i].y1 >> buffer[i].x2 >> buffer[i].y2; bool flag = dcmp(buffer[i].x1,buffer[i].x2); if( flag && buffer[i].y1>buffer[i].y2 ) swap(buffer[i].y1,buffer[i].y2); else if( !flag && buffer[i].x1>buffer[i].x2 ){ swap(buffer[i].x1,buffer[i].x2); swap(buffer[i].y1,buffer[i].y2); } buffer[i].k = flag ? INF : (buffer[i].y2-buffer[i].y1) / (buffer[i].x2-buffer[i].x1); buffer[i].b = flag ? buffer[i].x1 : buffer[i].y1-buffer[i].k*buffer[i].x1; } sort(buffer,buffer+n,cmp); for( int i=0 ; i<n-1 ; i++ ){ if( judge(buffer[i],buffer[i+1]) ){ re--; if( dcmp(buffer[i].k,INF) ) buffer[i+1].y2 = max(buffer[i].y2,buffer[i+1].y2); else buffer[i+1].x2 = max(buffer[i].x2,buffer[i+1].x2); } } cout << re << endl; } return 0; }