Poj 2907 Collecting Beepers【枚举+最短路径】
来源:互联网 发布:中国海洋大学就业 知乎 编辑:程序博客网 时间:2024/06/05 05:27
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 (i, j) to an adjacent position (i, j + 1), (i, j − 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 (x, y) 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
题意:
给出当前位置坐标,以及若干个物品的坐标,每次只能向上下左右四个方向走,问搜集所有的东西,最后走回到起点至少需要走多少次
题解:
首先明确,本题给出的条件限制可知,如果两个点的坐标知道了,那么两点间的距离是确定的
数据范围比较小,可以直接暴力,枚举收集各个物品的顺序,然后进行统计就行了...
本来是找哈密顿问题来做的,看到了这个题,随便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;}
- Poj 2907 Collecting Beepers【枚举+最短路径】
- POJ 2907 Collecting Beepers
- poj 2907 Collecting Beepers dfs
- POJ 2907 Collecting Beepers 笔记
- POJ 2907 Collecting Beepers DFS+回溯+剪枝
- POJ 2907 Collecting Beepers (DFS+回溯)
- POJ-2907(全排列->dfs)(Collecting Beepers)
- poj 2907 Collecting Beepers 邮递员问题暴力解法
- poj--1062(最短路径+枚举)
- POJ Collecting Beepers (dfs 深度搜索)
- POJ 1062 昂贵的聘礼(最短路径Dijkstra+枚举)
- POJ 1135 Domino Effect(最短路径+枚举)
- POJ 最短路径
- poj_1135_最短路径&枚举
- UVa 10496 - Collecting Beepers
- poj 最短路径 汇总
- POJ 3259 最短路径
- poj 1125 最短路径
- 强引用,软引用,弱引用,虚引用
- Android活动的生存期
- jQuery常用插件
- [Unity3D]简单使用Protobuf-net(二)
- POJ 3714(Raid-平面最近点对)
- Poj 2907 Collecting Beepers【枚举+最短路径】
- MySQL自定义函数
- Android 学习资料收集
- 关于Unicode编码的闲谈
- hdoj1002
- Spring核心之依赖注入(三)
- poj 1088 滑雪
- Android开发艺术探索学习笔记之Activity(1)生命周期
- OSGI入门环境搭建及相关概念