zoukankan      html  css  js  c++  java
  • poj2907

    枚举

    View Code
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;

    #define maxn 15

    struct Point
    {
    int x, y;
    }point[maxn], s;

    int n, f[maxn];

    void input()
    {
    scanf("%d%d", &s.x, &s.y);
    scanf("%d%d", &s.x, &s.y);
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    scanf("%d%d", &point[i].x, &point[i].y);
    }

    int dist(Point &a, Point &b)
    {
    return abs(a.x - b.x) + abs(a.y - b.y);
    }

    int cal()
    {
    int ret = dist(s, point[f[0]]);
    for (int i = 0; i < n - 1; i++)
    ret += dist(point[f[i]], point[f[i + 1]]);
    ret += dist(s, point[f[n - 1]]);
    return ret;
    }

    void work()
    {
    for (int i = 0; i < n; i++)
    f[i] = i;
    int ans = 0x3f3f3f3f;
    do
    {
    ans = min(ans, cal());
    }while (next_permutation(f, f + n));
    printf("The shortest path has length %d\n", ans);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
    input();
    work();
    }
    return 0;
    }

  • 相关阅读:
    LOL 计蒜客
    cf1486 D. Max Median
    P3567 [POI2014]KUR-Couriers
    dp 求物品组合情况
    黑暗爆炸
    hdu5306 Gorgeous Sequence
    P4609 [FJOI2016]建筑师
    cf 1342 E. Placing Rooks
    重修dp-背包
    acwing 2154. 梦幻布丁
  • 原文地址:https://www.cnblogs.com/rainydays/p/2200707.html
Copyright © 2011-2022 走看看