#include #include #include class Volcanoe { public: int x; int y; Volcanoe(int xx, int yy) { x = xx; y = yy; } }; int amount_of_volcanoes; //Volcanoe first_volcanoe = Volcanoe(-1, -1); //Volcanoe last_volcanoe = Volcanoe(-1, -1); //std::vector middle_volcanoes; std::vector volcanoes; void load_data() { scanf("%d", &amount_of_volcanoes); for (int i = 0; i < amount_of_volcanoes; i++) { int x, y; scanf("%d %d", &x, &y); /*if (i == 0) { first_volcanoe = Volcanoe(x, y); continue; } if (i == amount_of_volcanoes - 1) { last_volcanoe = Volcanoe(x, y); continue; }*/ volcanoes.push_back(Volcanoe(x, y)); } } int journey_lenght() { int actual_x = volcanoes[0].x; int actual_y = volcanoes[0].y; int length = 0; for (int i = 1; i < volcanoes.size(); i++) { //printf("x: %d, y: %d\n", volcanoes[i]); int next_x = volcanoes[i].x; int next_y = volcanoes[i].y; length += std::abs(actual_x - next_x); length += std::abs(actual_y - next_y); actual_x = volcanoes[i].x; actual_y = volcanoes[i].y; } return length; } int permute(int l, int r, int shortest) { if (l == r) { int journey = journey_lenght(); return (journey >= shortest) ? shortest : journey; } for (int i = l; i <= r; i++) { std::swap(volcanoes[l], volcanoes[i]); int tmp = permute(l + 1, r, shortest); if (tmp < shortest) shortest = tmp; std::swap(volcanoes[l], volcanoes[i]); } return shortest; } int main() { load_data(); //std::cout << shortest_journey() << '\n'; std::cout << permute(0, volcanoes.size() - 1, 2147483647) << '\n'; // INT_MAX /*for (int i = 0; i < volcanoes.size(); i++) { printf("x: %d, y: %d\n", volcanoes[i]); }*/ return 0; }