poj 2907

来源:互联网 发布:loop算法讲解 编辑:程序博客网 时间:2024/06/06 00:34

Collecting Beepers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2401 Accepted: 1296

Description

Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in her world. To do so you must direct Karel to the position where each beeper is located. Your job is to write a computer program that finds the length of the shortest path that will get Karel from her starting position, to each of the beepers, and return back again to the starting position.

Karel can only move along the x and y axis, never diagonally. Moving from one position (ij) to an adjacent position (ij + 1), (ij − 1), (i − 1, j), or (i + 1, j) has a cost of one.

You can assume that Karel’s world is never larger than 20 × 20 squares and that there will never be more than 10 beepers to pick up. Each coordinate will be given as a pair (xy) where each value will be in the range 1 through the size of that particular direction of the coordinate system.

Input

First there will be a line containing the number of scenarios you are asked to help Karel in. For each scenario there will first be a line containing the size of the world. This will be given as two integers (x-size andy-size). Next there will be one line containing two numbers giving the starting position of Karel. On the next line there will be one number giving the number of beepers. For each beeper there will be a line containing two numbers giving the coordinates of each beeper.

Output

The output will be one line per scenario, giving the minimum distance that Karel has to move to get from her starting position to each of the beepers and back again to the starting position.

Sample Input

110 101 142 35 59 46 5

Sample Output

The shortest path has length 24

从起点开始,遍历完n个点,最后回到起点,求最小的路径

直接枚举,将n个点全排列,从而计算每个环的总长度,取最小的长度即可。

利用STL里的next_permutation

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>using namespace std;struct LL{                ///存点的坐标   int x,y;}w[13];int m,a[13];int dis(LL &n1,LL &n2)             ///两点之间的距离{    return abs(n1.x-n2.x)+abs(n1.y-n2.y);}int deal()                /// 每一种情况下的最短路径{    int i,ans=0;    ans=dis(w[0],w[a[1]]);    for(i=1;i<m;i++)        ans+=dis(w[a[i]],w[a[i+1]]);    ans+=dis(w[0],w[a[m]]);    return ans;}int main(){    int i,T;    scanf("%d",&T);    while(T--){       scanf("%d%d",&w[0].x,&w[0].y);       /// 这一组数据没用       scanf("%d%d",&w[0].x,&w[0].y);       scanf("%d",&m);     /// m 点的个数       for(i=1;i<=m;i++)          scanf("%d%d",&w[i].x,&w[i].y);       for(i=1;i<=m;i++)  /// a[i] 初始化 存点的序号           a[i]=i;       int ans=0x3f3f3f3f;       do{                                ///枚举每一种情况         ans=min(ans,deal());       }while(next_permutation(a+1,a+1+m));  /// a[i] 全排列,next_permutation是当排列最大时返回假,                                            /// 故 数组a的初值一定要是最小到大的,才能保证枚举完所有排列      cout<<"The shortest path has length "<<ans<<endl;    }    return 0;}



0 0