POJ Collecting Beepers (dfs 深度搜索)

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

Source



题意:就是给你图的范围(无用),给你起点,然后n个点,求起点开始每个点都走,再回起点,的最小距离,不过有点特别,

就是两个点的距离是曼哈顿距离,就是fabs(x-x)+fabs(y-y)


今天比赛的时候最后一名,一直在做h题,两个小时无果%>_<%,这个人都不好了,队长让我做这个题,我急着做h结果想着快点做,结果居然1A了,心情好了一点,

不过h还是没有a,还是倒数第一,哎。。。菜比的苦谁能体会



注意的一点,用G++交,用c++会编译错误


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define N 30

struct stud{
int x,y;
}f[N*N];

int sx,sy;
int ans;
int n,m;
int vis[N*N];


void dfs(int x,int y,int pos,int temp)  //x,y是是上一个点的x,y,temp是前pos个点走的距离
{
    if(pos==n)  //这个解释在下面
    {
        if(temp+fabs(x-sx)+fabs(y-sy)<ans)
            ans=temp+fabs(x-sx)+fabs(y-sy);
        return ;
    }

    int i;
    for(i=0;i<n;i++)
        if(!vis[i])
    {
        vis[i]=1;
        dfs(f[i].x,f[i].y,pos+1,temp+fabs(f[i].x-x)+fabs(f[i].y-y));
        vis[i]=0;
    }
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        ans=0x3f3f3f3f;
        scanf("%d%d",&n,&m);

        scanf("%d%d",&sx,&sy);

        scanf("%d",&n);

        for(i=0;i<n;i++)
            scanf("%d%d",&f[i].x,&f[i].y);

        memset(vis,0,sizeof(vis));
        dfs(sx,sy,0,0);  //因为从0开始,所以pos为n时已经是n+1个点了,就是回起点

    printf("The shortest path has length %d\n",ans);
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值