# POJ 2907 Collecting Beepers DFS+回溯+剪枝

1417人阅读 评论(0)

http://poj.org/problem?id=2907

Collecting Beepers
 Time Limit: 1000MS Memory Limit: 65536K

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

1
10 10
1 1
4
2 3
5 5
9 4
6 5

Sample Output

The shortest path has length 24
/* Author : yan
* Question : POJ 2907 Collecting Beepers
* Date && Time : Sunday, December 26 2010  10:14 PM
* Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include<stdio.h>
#define bool _Bool
#define false 0
#define true 1
bool visited[21];
int mapx[21],mapy[21];
int x_size,y_size;
int start_x,start_y;

int beepers;

int ans;///答案

int cnt;///当前捡起的beeper数目
int path;///当前的路径长度

void DFS(int a,int b)
{
int cache;///临时变量
if(cnt==beepers)
{
cache=path+abs(a-start_x)+abs(b-start_y);///往返路程，由于当全部捡起时回到原点的最短路径就是abs(a-start_x)+abs(b-start_y)
if(ans>cache) ans=cache;
return;
}
int i;
for(i=0;i<beepers;i++)
{
if( visited[i] ) continue;///剪枝
visited[i]=true;
cache=abs(mapx[i]-a)+abs(mapy[i]-b);
cnt++;
path+=cache;
DFS(mapx[i],mapy[i]);
path-=cache;///回溯
cnt--;
visited[i]=false;
}
}
int main()
{
freopen("input","r",stdin);
int test;

int i;

int x,y;
scanf("%d",&test);
while(test--)
{
memset(visited,false,sizeof(visited));
path=0;
cnt=0;
ans=999999;
scanf("%d %d",&x_size,&y_size);
scanf("%d %d",&start_x,&start_y);
scanf("%d",&beepers);
for(i=0;i<beepers;i++)
{
scanf("%d %d",&mapx[i],&mapy[i]);
}
DFS(start_x,start_y);
printf("The shortest path has length %d/n",ans);
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：2415214次
• 积分：27311
• 等级：
• 排名：第211名
• 原创：383篇
• 转载：106篇
• 译文：3篇
• 评论：652条
博客专栏
 Linux内核网络栈源代码分析 文章：17篇 阅读：213478
 LaTeX使用 文章：5篇 阅读：67753
 Gentoo Linux使用技巧 文章：27篇 阅读：156355
 Linux内核学习笔记 文章：53篇 阅读：357235
 ARM-Linux驱动移植 文章：19篇 阅读：132150
 ARM-Linux驱动开发 文章：17篇 阅读：139229
评论排行
最新评论