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

posted @ 2011-10-07 18:52  金海峰  阅读(201)  评论(0编辑  收藏  举报