i conduct

转载 2015年11月20日 22:46:01
  1. I Conduit!

Constraints

Time Limit: 3 secs, Memory Limit: 32 MB

Description

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.

Input

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.

Output

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.

Sample Input

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
Sample Output

2
1
2

CPP

#include <iostream>
#include <cmath>
#include <algorithm>

double const MAX = 99999;

using namespace std;

struct seg{//结构体的标签
    double k;
    double b;
    double leftx;
    double downy;
    double rightx;
    double upy;
}l[10001];//一个数组;记住如何定义结构体

void swap(double& a, double& b) {
    double tmp = a;
    a = b;
    b = tmp;
}
/*void swap(double *a, double *b) {
    double tmp = *a;
    a = b;
    b = &tmp;
    用c来表示swap函数
}*/


bool equal(double a, double b) {
    return fabs(a-b) < 1e-6;
}//浮点数有误差,当误差小于某个数时,认为这两个数是相等的。

bool comseg(const seg& a, const seg& b) {
    if(equal(a.k, b.k)) {
        if(a.k == MAX) {
            if(equal(a.b, b.b)) {
                if(equal(a.downy, b.upy)) {
                    return a.upy < b.upy;
                }
                return a.downy < b.downy;
            }
            return a.b < b.b;
        }
        else if(equal(a.b, b.b)) {
            if(equal(a.leftx, b.leftx)) {
                return a.rightx < b.rightx;
            }
            return a.leftx < b.leftx;
        }
        return a.b < b.b;
    }
    return a.k < b.k;
}//用于排序!

int main() {
    int n;
    while(cin >> n && n != 0) {
        for(int i = 0; i < n; i++) {
            cin >> l[i].leftx >> l[i].downy >> l[i].rightx >> l[i].upy;
            if(l[i].leftx == l[i].rightx) {
                l[i].k = MAX;
                l[i].b = l[i].leftx;
                if(l[i].downy > l[i].upy) {
                    swap(l[i].downy, l[i].upy);//让up和right为大的数。
                }
            }
            else if(l[i].leftx > l[i].rightx) {
                l[i].k = (l[i].upy - l[i].downy) / (l[i].rightx - l[i].leftx);
                l[i].b = l[i].downy - l[i].leftx  * l[i].k;
                swap(l[i].leftx, l[i].rightx);
                swap(l[i].downy, l[i].upy);
            }
            else {
                l[i].k = (l[i].upy - l[i].downy) / (l[i].rightx - l[i].leftx);
                l[i].b = l[i].downy - l[i].leftx  * l[i].k;
            }
        }
        sort(l, l+n, comseg);
        int count = 1;
        double x = l[0].rightx;
        double y = l[0].upy;
        for(int i = 1; i < n; i++) {
            if(!(equal(l[i].k, l[i-1].k) && equal(l[i].b, l[i-1].b))){//注意!的作用域!
                count++;
            }
            else if((l[i].k == MAX && l[i].downy > y) || (l[i].k != MAX && l[i].leftx > x)) {
                count++;
            }
            else if((l[i].k == MAX && y > l[i].upy) || (l[i].k != MAX && x >= l[i].rightx)) {
                continue;
            }
            x = l[i].rightx;
            y = l[i].upy;
        }
        cout << count << endl;
    }
    return 0;
}

此题思路:
1一定要用排序!可以减少很多的问题,只要比较相邻两条线就可以了。
2就算k和d相等,也有需要比较的部分。
3注意浮点数的误差问题

Ubuntu麒麟社区的行为准则(Code of Conduct)

     4月14日,我进入UbuntuKylin中文社区(www.ubuntukylin.com)的技术论坛,有感。 UbuntuKylin社区的官方口号是:“做更有中国味的操作系...

JS操作数组循环删除指定元素,splice(i,1)出错解决方案

test,test,test,这里是摘要!
  • sRhee
  • sRhee
  • 2017年04月27日 16:38
  • 273

FANUC 0i MF 简明调试手册

  • 2017年12月11日 22:28
  • 8.06MB
  • 下载

[计算机组成原理][R-I-J型指令CPU设计实验总结]

总算解决一大心头之患了,比想象中容易,通宵两夜,刷完了十个实验,这个实验就是最后的了。感慨颇多。特地写篇总结。 想做一件事,就立马去做把。你会发现没那么困难,往往最大的困难,是心里的困难。 培养了HD...

linux I2C通用驱动

  • 2017年12月05日 17:00
  • 30KB
  • 下载

Core Java Volume I--Fundamentals(10th) epub

  • 2017年11月29日 16:47
  • 83.51MB
  • 下载

STM32学习之路:I2C的基本读写

用stm32f4使用I2C的基本读写操作
  • Lxingmo
  • Lxingmo
  • 2017年04月10日 16:49
  • 1897
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:i conduct
举报原因:
原因补充:

(最多只允许输入30个字)