Cod sursa(job #1680218)

Utilizator AndreiFlorescuAndrei Florescu AndreiFlorescu Data 8 aprilie 2016 16:22:38
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.97 kb
#include <iostream>
#include <fstream>
#include <climits>

using namespace std;

struct cell {
    int x1;
    int y1;
    int x2;
    int y2;
    bool dir;
};

cell seg[1002], aux;
int dist[1002][1002];
int nr;

int MAX1(int a, int b) {
    return a > b ? a : b;
}

int MAX (int a, int b, int c, int d) {
    return MAX1(MAX1(a, b), MAX1(c, d));
}

int MIN (int a, int b) {
    if (a > b) {
        return b;
    }
    return a;
}

int maxim (int a, int b) {
    if (a > b) {
        return a;
    }
    return b;
}

int timpMin (int i, int j) {
    int aux;
    if (seg[i].dir != 1) {
        aux = i;
        i = j;
        j = aux;
    }
    if (seg[i].x1 <= seg[j].x1 && seg[j].x1 <= seg[i].x2) {
        if (seg[j].y1 <= seg[i].y1 && seg[i].y1 <= seg[j].y2) {
            return 0;
        } else if (seg[j].y1 > seg[i].y1) {
            return seg[j].y1 - seg[i].y1;
        } else {
            return seg[i].y1 - seg[j].y2;
        }
    } else {
        if (seg[j].y1 < seg[i].y1 && seg[i].y1 < seg[j].y2) {
            if (seg[i].x1 > seg[j].x1) {
                return seg[i].x1 - seg[j].x1;
            } else {
                return seg[j].x1 - seg[i].x2;
            }
        } else {
            if (seg[i].x1 > seg[j].x1) {
                if (seg[j].y1 > seg[i].y1) {
                    return maxim(seg[j].y1 - seg[i].y1, seg[i].x1 - seg[j].x1);
                } else {
                    return maxim(seg[i].y1 - seg[j].y2, seg[i].x1 - seg[j].x1);
                }
            } else {
                if (seg[j].y1 > seg[i].y1) {
                    return maxim(seg[j].y1 - seg[i].y1, seg[j].x1 - seg[i].x2);
                } else {
                    return maxim(seg[i].y1 - seg[j].y2, seg[j].x1 - seg[i].x2);
                }
            }
        }
    }
}

int main() {
    ifstream file_in ("segmente.in");
    ofstream file_out ("segmente.out");

    int i, j, k;
    int minim, secund;

    // Citirea datelor
    file_in >> nr;
    for (i = 0; i < nr; i++) {
        file_in >> aux.y1 >> aux.x1 >> aux.y2 >> aux.x2;
        if (aux.x1 == aux.x2) {
            seg[i].dir = 0;
            seg[i].x1 = seg[i].x2 = aux.x1;
            if (aux.y1 < aux.y2) {
                seg[i].y1 = aux.y1;
                seg[i].y2 = aux.y2;
            } else {
                seg[i].y1 = aux.y2;
                seg[i].y2 = aux.y1;
            }
        } else {
            seg[i].dir = 1;
            seg[i].y1 = seg[i].y2 = aux.y1;
            if (aux.x1 < aux.x2) {
                seg[i].x1 = aux.x1;
                seg[i].x2 = aux.x2;
            } else {
                seg[i].x1 = aux.x2;
                seg[i].x2 = aux.x1;
            }
        }
    }
    file_in.close();

    // Calcularea solutiei
    for (i = 0; i < nr; i++) {
        for (j = i + 1; j < nr; j++) {
            if (seg[i].dir != seg[j].dir) {
                dist[i][j] = dist[j][i] = timpMin(i, j);
            } else {
                dist[j][i] = dist[i][j] = 0;
            }
        }
    }

    for (i = 0; i < nr; i++) {
        for (j = 0; j < nr; j++) {
            file_out << dist[i][j] << " ";
        }
        file_out << "\n";
    }

    int sol = INT_MAX;
    for (i = 0; i < nr - 1; i++) {
        if (seg[i].dir == 1) {
            for (j = i + 1; j < nr; j++) {
                if (seg[j].dir == 1) {
                    secund = minim = INT_MAX;
                    for (k = 0; k < nr; k++) {
                        if (seg[k].dir == 0) {
                            if (maxim(dist[i][k], dist[j][k]) <= minim) {
                                secund = minim;
                                minim = k;
                            } else if (maxim(dist[i][k], dist[j][k]) < secund) {
                                secund = k;
                            }
                        }
                    }
                    sol = MIN(sol, MAX(dist[i][minim], dist[j][minim], dist[i][secund], dist[j][secund]));
                }
            }
        }
    }

    // Afisarea solutiei
    file_out << sol;

    return 0;
}