poj2907

枚举

ContractedBlock.gif ExpandedBlockStart.gif 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;
}

转载于:https://www.cnblogs.com/rainydays/archive/2011/10/07/2200707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值