POJ 2907 Collecting Beepers

Collecting Beepers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2236 Accepted: 1214

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

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

Sample Output

The shortest path has length 24
 
题意: 在一个表格里给出几个点的坐标 从开始点出发,分别经过给出的坐标点,并且在经过最后的一个点的时候再返回到开始点,问最短的步数是多少,只能上下左右移动……
  题解: 用DFS深搜,分别搜到每一个点,最后求最短的路径……(开始的时候用BFS做的,本来想可以进行全排列后 ,找出从一个点到下一个点的最短路径 ,最后再加起来,若起点在给出的点的中间的话,并不符合题意,所以用DFS直接遍历每一个给出的坐标,最后在找出最短路径……)
 
注意: 二维数组的全排列好长时间不用给忘了……
 
我的代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<cmath>
using namespace std;
struct node
{

    int x,y;
};
int X,Y;
int N;
int maxstep;
const int maxn=100;
int vis[300];
int map[maxn][maxn];
node ps[maxn];
int distance1(int i,int j)
{
    int dx=ps[i].x-ps[j].x;
    int dy=ps[i].y-ps[j].y;
    dx=dx>0?dx:-dx;
    dy=dy>0?dy:-dy;
    return dx+dy;
}

void  dis(int X,int Y)
{
    int m,n;
    for (m=0;m<X;m++)
    {
        for (n=0;n<Y;n++)
        {
            map[m][n]=distance1(m,n);
        }
    }
}

inline int dist(int i, int j)
{
    return map[i][j];
}

void dfs(const int start,const int level,const int disyuan)
{
	
    if (level==N)
    {
        int temp = disyuan + dist(start, 0);
        maxstep = temp< maxstep ?temp:maxstep;
        return;
    }
    for (int i=1;i<=N;i++)
    {
        if (vis[i]==0)
        {
            vis[i]=1;
            dfs(i,level+1,disyuan+dist(i,start));
            vis[i]=0;
        }

    }
}
int main()
{
    int T;

    cin>>T;
    while (T--)
    {
        cin>>X>>Y;
       
        cin>>ps[0].x>>ps[0].y;
        cin>>N;
        for (int i=1;i<=N;i++)
        {
            cin>>ps[i].x>>ps[i].y;
            //ps[i].vis=0;
        }
        dis(X,Y);//存图
        maxstep=999999999;
        dfs(0,0,0);

        cout<<"The shortest path has length "<<maxstep<<endl;

    }

}

 

另一个比较简单的代码,不是我写的:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct node
{
    int x;
    int y;
}p[500];
int len,tmp,sx,sy;
int n,vis[500];
void dfs(int nowL,int num,int x,int y)//目前长度,数量,现在的位置坐标
{
    if(num==n)
    {
        len=abs(sx-x)+abs(sy-y);
        len+=nowL;
        if(len<tmp)//回溯长度比较
        tmp=len;
        return ;
    }
    for(int i=0;i<n;i++)
    if(!vis[i])//未被访问
    {
        vis[i]=1;
        len=abs(p[i].x-x)+abs(p[i].y-y);
        dfs(len+nowL,num+1,p[i].x,p[i].y);
        vis[i]=0;// 回溯
    }
}
int main()
{
    int t;
    int mx,my;
    cin>>t;
    while(t--)
    {
        len=0;
        tmp=9999999;
        memset(vis,0,sizeof(vis));
        cin>>mx>>my;
        cin>>sx>>sy;
        cin>>n;
        for(int i=0;i<n;i++)
        cin>>p[i].x>>p[i].y;
        dfs(0,0,sx,sy);
        cout<<"The shortest path has length "<<tmp<<endl;
    }
    return 0;
}


 

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值