Sicily 1004. I Conduit!

1004. 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
惨不忍睹的WA在了精度问题上。。
题目意思:给你很多线段,问你在同一条直线上且有重复部分(点重复也算,注意不是相交)的线段合并后剩下多少条线段。
按照一定的优先次序将线段排序,然后依次比较是否是重合线段即可。
这题的精度一定要注意。

先给出自己写的,略麻烦了点:

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;

#define MAX 10005
#define DEV 1e-6
#define INF 99999999

vector<double> line[MAX];  // k, b, x1, y1, x2, y2, x1 <= x2
bool isSame[MAX];

inline bool doubleIsSame(double a, double b) {
    return fabs(a - b) < DEV;
}

bool cmp(vector<double> a, vector<double> b) {
    if (!doubleIsSame(a[0], b[0])) return a[0] < b[0];
    else if (!doubleIsSame(a[1], b[1])) return a[1] < b[1];
    else if (a[2] != b[2]) return a[2] < b[2];
    else return a[3] < b[3];
}

int main() {

    std::ios::sync_with_stdio(false);

    while (1) {
        int n;
        cin >> n;
        if (!n) break;
        for (int i = 0; i < n; i++) {
            isSame[i] = false;
            line[i].resize(6);
            cin >> line[i][2] >> line[i][3] >> line[i][4] >> line[i][5];
            if (line[i][2] > line[i][4]) {
                swap(line[i][2], line[i][4]);
                swap(line[i][3], line[i][5]);
            }
            if (line[i][2] == line[i][4]) {
                line[i][0] = INF;
                line[i][1] = line[i][2];
                if (line[i][3] > line[i][5]) {
                    swap(line[i][3], line[i][5]);
                }
                continue;
            }
            line[i][0] = (line[i][5] - line[i][3]) / (line[i][4] - line[i][2]);
            line[i][1] = line[i][3] - line[i][2] * line[i][0];
        }
        sort(line, line + n, cmp);
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (!isSame[i]) {
                ans++;
                for (int j = i + 1; j < n; j++) {
                    if (!doubleIsSame(line[i][0], line[j][0])) break;
                    if (!doubleIsSame(line[i][1], line[j][1])) break;
                    if (line[i][0] == INF) {
                        if ((line[i][3] <= line[j][3] && line[j][3] <= line[i][5]) || (line[i][3] <= line[j][5] && line[j][5] <= line[i][5]) || (line[j][3] <= line[i][3] && line[i][3] <= line[j][5]) || (line[j][3] <= line[i][5] && line[i][5] <= line[j][5])) {
                            isSame[j] = true;
                            line[i][3] = min(line[i][3], line[j][3]);
                            line[i][5] = max(line[i][5], line[j][5]);
                        }
                        continue;
                    }
                    if ((line[i][2] <= line[j][2] && line[j][2] <= line[i][4]) || (line[i][2] <= line[j][4] && line[j][4] <= line[i][4]) || (line[j][2] <= line[i][2] && line[i][2] <= line[j][4]) || (line[j][2] <= line[i][4] && line[i][4] <= line[j][4])) {
                        isSame[j] = true;
                        line[i][2] = min(line[i][2], line[j][2]);
                        line[i][4] = max(line[i][4], line[j][4]);
                    }
                }
            }
        }
        cout << ans << endl;
    }

    //getchar();
    //getchar();
    
    return 0;
}             

下面这份是一直WA后参考网上的(其实是因为精度- -)
// Problem#: 1004
// Submission#: 3135831
// 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 <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;

#define MAX 10005
#define DEV 1e-6
#define INF 99999

vector<double> line[MAX];  // k, b, x1, y1, x2, y2, x1 <= x2

inline bool doubleIsSame(double a, double b) {
    return fabs(a - b) < DEV;
}

bool cmp(vector<double> a, vector<double> b) {
    if (!doubleIsSame(a[0], b[0])) return a[0] < b[0];
    else {
        if (a[0] == INF) {
            if (!doubleIsSame(a[1], b[1])) return a[1] < b[1];
            if (!doubleIsSame(a[3], b[3])) return a[3] < b[3];
            return a[5] < b[5];
        } else {
            if (!doubleIsSame(a[1], b[1])) return a[1] < b[1];
            if (!doubleIsSame(a[2], b[2])) return a[2] < b[2];
            return a[4] < b[4];
        }
    }
}

int main() {

    std::ios::sync_with_stdio(false);

    while (1) {
        int n;
        cin >> n;
        if (!n) break;
        for (int i = 0; i < n; i++) {
            line[i].resize(6);
            double x1, x2, y1, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if (x1 > x2 || (x1 == x2 && y1 > y2)) {
                swap(x1, x2);
                swap(y1, y2);
            }
            line[i][2] = x1;
            line[i][3] = y1;
            line[i][4] = x2;
            line[i][5] = y2;
            if (x1 != x2) {
                line[i][0] = (y2 - y1) / (x2 - x1);
                line[i][1] = y1 - line[i][0] * x1;
            } else {
                line[i][0] = INF;
                line[i][1] = x1;
            }
        }
        sort(line, line + n, cmp);
        int ans = 1;
        double x = line[0][4], y = line[0][5];
        for (int i = 1; i < n; i++) {
            if (!doubleIsSame(line[i][0], line[i - 1][0]) || !doubleIsSame(line[i][1], line[i - 1][1])) {
                ans++;
                x = line[i][4];
                y = line[i][5];
            } else if ((line[i][0] != INF && x < line[i][2]) || (line[i][0] == INF && y < line[i][3])) {
                ans++;
                x = line[i][4];
                y = line[i][5];
            } else if ((line[i][0] != INF && x < line[i][4]) || (line[i][0] == INF && y < line[i][5])) {
                x = line[i][4];
                y = line[i][5];
            }
        }
        cout << ans << endl;
    }

    //getchar();
    //getchar();
    
    return 0;
}                   



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值