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;
    }

  • 相关阅读:
    mysql事务
    mysql函数
    mysql自连接
    MYSQL添加外键关联
    SQL多表查询
    SQL数据完整性
    SQL limit
    SQL分组查询
    升级GCC 6.2编译LLVM的问题
    Quartz时SLF4J错误
  • 原文地址:https://www.cnblogs.com/rainydays/p/2200707.html
Copyright © 2011-2022 走看看