2018北航计算机学院上机复试题一

这是一种暴力破解法。
#include 

#define max(a, b) a > b? a: b

typedef struct line {
    int s_x, s_y;
    int e_x, e_y;
    int length;
    int flag;
} Line;

Line lines[100];

int main() {
    int n;
    scanf("%d", &n);
    int x1, x2, y1, y2;
    int maxLength = 1;
    for (int i = 0; i < n; ++i) {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        lines[i].flag = 1;
        if (i == 0) {
            lines[i].s_x = x1;
            lines[i].s_y = y1;
            lines[i].e_x = x2;
            lines[i].e_y = y2;
            lines[i].length = 1;
        } else {
            int f = 0, b = 0;
            int j, k;
            for (j = 0; j < i; ++j) {
                if (x1 == lines[j].e_x && y1 == lines[j].e_y && lines[j].flag) {
                    f = 1;
                    break;
                }
            }
            for (k = 0; k < i; ++k) {
                if (x2 == lines[k].s_x && y2 == lines[k].s_y && lines[k].flag) {
                    b = 1;
                    break;
                }
            }
            if (f == 1 && b == 1) {
                lines[i].s_x = lines[j].s_x;
                lines[i].s_y = lines[j].s_y;
                lines[i].e_x = lines[k].e_x;
                lines[i].e_y = lines[k].e_y;
                lines[j].flag = 0;
                lines[k].flag = 0;
                lines[i].length = lines[j].length + lines[k].length + 1;
            } else if (f == 1 && b == 0) {
                lines[i].s_x = lines[j].s_x;
                lines[i].s_y = lines[j].s_y;
                lines[i].e_x = x2;
                lines[i].e_y = y2;
                lines[j].flag = 0;
                lines[i].length = lines[j].length + 1;
            } else if (f == 0 && b == 1) {
                lines[i].e_x = lines[k].e_x;
                lines[i].e_y = lines[k].e_y;
                lines[i].s_x = x1;
                lines[i].s_y = y1;
                lines[k].flag = 0;
                lines[i].length = lines[k].length + 1;
            } else {
                lines[i].s_x = x1;
                lines[i].s_y = y1;
                lines[i].e_x = x2;
                lines[i].e_y = y2;
                lines[i].length = 1;
            }
        }
        maxLength = max(maxLength, lines[i].length);
    }
    for (int i = 0; i < n; ++i)
        if (lines[i].flag && lines[i].length == maxLength) {
            printf("%d %d %d", lines[i].length, lines[i].s_x, lines[i].s_y);
        }
}

你可能感兴趣的:(2018北航计算机学院上机复试题一)