POJ 2907 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

Svenskt M?sterskap i Programmering/Norgesmesterskapet 2002

简单的DFS暴搜,操,但是做的时候不知道怎么想的。直接暴力枚举所有情况取最小值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
int tt,n,m,s,t,x;
int cnt,ans,sum;//cnt记录了多少点,ans记录答案,sum记录中间的可行答案
int visit[550];
struct node{
    int x,y;;
}e[550];
void dfs(int xx,int yy)
{
    if(sum>ans)//剪枝
        return ;
//    cout<<"cnt "<<cnt<<endl;
//    cout<<"sum "<<sum<<endl;
    if(cnt==x)
    {
//        cout<<"fuck "<<sum<<endl;
        int temp=sum+abs(xx-s)+abs(yy-t);
        if(temp<ans)
           ans=temp;
        return ;
    }
    for(int i=0;i<x;i++)
    {
        if(!visit[i])
        {
            int temp=abs(e[i].x-xx)+abs(e[i].y-yy);
            visit[i]=1;
            cnt++;
            sum+=temp;
            dfs(e[i].x,e[i].y);
            cnt--;
            sum-=temp;
            visit[i]=0;
        }
    }
}
int main()
{
   cin>>tt;
   while(tt--)
   {
       cin>>n>>m>>s>>t>>x;
       for(int i=0;i<x;i++)
          cin>>e[i].x>>e[i].y;
//       cout<<s<<" "<<t<<endl;
//       cout<<"s "<<x<<endl;
       ans=INT_MAX;
       cnt=0;
       sum=0;
       dfs(s,t);
       printf("The shortest path has length %d\n",ans);
   }
}

POJ 2907 Collecting Beepers (DFS+回溯)

时间: 2024-10-25 11:55:03

POJ 2907 Collecting Beepers (DFS+回溯)的相关文章

poj 2907 Collecting Beepers 邮递员问题暴力解法

题意: 给起点和n个点,求从起点出发经过这n个点每个点至少一次再回到起点的最短路. 分析: 类似邮递员问题,直接用STL枚举访问顺序暴力解决. 代码: //poj 2907 //sep9 #include <iostream> #include <algorithm> using namespace std; int x[16],y[16]; int d[16][16]; int a[16]; int n; int main() { int cases; scanf("%

POJ 1321 棋盘问题(dfs回溯)

A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1321 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每

poj 1154 letters (dfs回溯)

http://poj.org/problem?id=1154 #include<iostream> using namespace std; int bb[26]={0},s,r,sum=1,s1=1; char aa[25][25]; int dir[4][2]={-1,0,1,0,0,-1,0,1}; void dfs(int a,int b) { int a1,b1; if(s1>sum) sum=s1; //更新最大数值 for(int i=0;i<4;i++) { a1=

POJ 3740 Easy Finding(dfs回溯)

B - Easy Finding Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3740 Description Given a M× N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn

POJ 1321 棋盘问题 (DFS + 回溯)

题目链接:http://poj.org/problem?id=1321 题意:中文题目,就不多说了...... 思路: 解题方法挺多,刚开始想的是先从N行中选择出来含有“#”的K行,再在这K行中放置K个棋子,就好了.时间复杂度为O( C(n, k)  *  k! ),真实写的时候其实用了2N * k!,勉强也过了.后面又想到可以先从第一个出现的“#”开始搜,搜完之后直接跳到下一行继续,就不用第一次做那么麻烦了. 代码: (1) 1 #include <iostream> 2 #include

POJ 1129 Channel Allocation DFS 回溯

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 7871 Description When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a s

POJ 3411 Paid Roads(dfs)

*注:这一题很重要的是对与数据的处理和细节的把握把! http://poj.org/problem?id=3411 题目大意: 有n个城市,m条路,(0<=n,m<=10).从a到b,如果之前已经经过c点,那么付费p,否者付费r.求最小的费用,从1-->n! 注意: There may be more than one road connecting one city with another. so:你不能用map[a][b]存a->b的距离.只能有road [ i ]了. 还有

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

poj1321——dfs回溯

POJ 1321  DFS回溯+递归枚举 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24813   Accepted: 12261 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行