Poj 2907 Collecting Beepers【枚举+最短路径】

来源:互联网 发布:中国海洋大学就业 知乎 编辑:程序博客网 时间:2024/06/05 05:27

Collecting Beepers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2534 Accepted: 1369

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 and y-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

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2002

题意:

给出当前位置坐标,以及若干个物品的坐标,每次只能向上下左右四个方向走,问搜集所有的东西,最后走回到起点至少需要走多少次


题解:

首先明确,本题给出的条件限制可知,如果两个点的坐标知道了,那么两点间的距离是确定的

数据范围比较小,可以直接暴力,枚举收集各个物品的顺序,然后进行统计就行了...

本来是找哈密顿问题来做的,看到了这个题,随便A掉了....


/*http://blog.csdn.net/liuke19950717*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=0x3f3f3f3f;struct node {int x,y;};int dis(node a,node b){return abs(a.x-b.x)+abs(a.y-b.y);}int main(){int t;scanf("%d",&t);while(t--){int n,m;node x[25*25]={0};scanf("%d%d",&n,&m);scanf("%d%d",&x[0].x,&x[0].y);//起点int num,id[25*25]={0};scanf("%d",&num);x[num+1]=x[0];//注意要回到起点for(int i=1;i<=num;++i){scanf("%d%d",&x[i].x,&x[i].y);id[i]=i;//点的编号,也是点的下标}int ans=maxn;do{int sum=0;for(int i=1;i<=num+1;++i){sum+=dis(x[id[i]],x[id[i-1]]);}ans=min(ans,sum);}while(next_permutation(id+1,id+num+1));//枚举所有的可能性printf("The shortest path has length %d\n",ans);}return 0;}


0 0
原创粉丝点击