程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ Collecting Beepers (dfs 深度搜索)

POJ Collecting Beepers (dfs 深度搜索)

編輯:C++入門知識

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 (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

Source

Svenskt M?sterskap i Programmering/Norgesmesterskapet 2002



題意:就是給你圖的范圍(無用),給你起點,然後n個點,求起點開始每個點都走,再回起點,的最小距離,不過有點特別,

就是兩個點的距離是曼哈頓距離,就是fabs(x-x)+fabs(y-y)


今天比賽的時候最後一名,一直在做h題,兩個小時無果%>_<%,這個人都不好了,隊長讓我做這個題,我急著做h結果想著快點做,結果居然1A了,心情好了一點,

不過h還是沒有a,還是倒數第一,哎。。。菜比的苦誰能體會



注意的一點,用G++交,用c++會編譯錯誤


#include
#include
#include
#include
#include
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)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved