Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stack>
- #include <queue>
- #include <set>
- #include <map>
- #include <string>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- struct P {
- int x, y;
- P(int X = 0, int Y = 0) : x(X), y(Y) {}
- } p[4010];
- struct R {
- P a, b;
- R(P A = P(), P B = P()) : a(A), b(B) {}
- int len() {
- return abs(a.x - b.x) + abs(a.y - b.y);
- }
- bool isHoriz() {
- return a.y == b.y;
- }
- bool between(int x, int a, int b) {
- return ((a <= x && x <= b) || (a >= x && x >= b));
- }
- pair<bool, P> intersects(R &r) {
- if (isHoriz() == r.isHoriz())
- return make_pair(0, P());
- if (isHoriz()) {
- if (between(r.a.x, a.x, b.x) && between(a.y, r.a.y, r.b.y))
- return make_pair(1, P(r.a.x, a.y));
- } else {
- if (between(r.a.y, a.y, b.y) && between(a.x, r.a.x, r.b.x))
- return make_pair(1, P(a.x, r.a.y));
- }
- return make_pair(0, P());
- }
- } r[4010];
- int n, l[4000];
- vector<int> ans;
- int sLen(int ra, int rb) {
- return l[rb] - (ra ? l[ra - 1] : 0);
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- scanf("%d", &n);
- for (int i = 0; i < n; i++)
- scanf("%d%d", &p[i].x, &p[i].y);
- for (int i = 0; i < n - 1; i++) {
- r[i] = R(p[i], p[i + 1]);
- l[i] = r[i].len() + (i ? l[i - 1] : 0);
- for (int j = 0; j < i - 1; j++) {
- pair <bool, P> inter = r[i].intersects(r[j]);
- if (inter.first)
- ans.push_back(sLen(j + 1, i - 1) + R(r[i].a, inter.second).len() + R(inter.second, r[j].b).len());
- }
- }
- ans.push_back(l[n - 2]);
- printf("%d", *min_element(ans.begin(), ans.end()));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement