POJ Collecting Beepers (dfs Deep Search)

Source: Internet
Author: User

POJ Collecting Beepers (dfs Deep Search)

Description

Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (XAndY). 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 Fig 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 andY-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

110 101 142 35 59 46 5

Sample Output

The shortest path has length 24

Source

Svenskt M? Sterskap I Programmering/Norgesmesterskapet 2002



Meaning: it is the minimum distance between the range (useless) of the graph, the start point, and the n points,

That is, the distance between two points is the distance between Manhattan, that is, fabs (x-x) + fabs (y-y)


At the end of the competition today, I had been doing questions h, and I had no results for two hours. % >_<%, this person is not good. The Captain asked me to do this, I am eager to do the h result and want to do it quickly. The result is actually 1A, so I feel better,

But h still does not have a, or the last one, ah... Who can appreciate the bitter food ratio?



Note: If you use G ++ for cross, c ++ will cause compilation errors.


# 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 is the x, y, temp is the distance between the previous pos points {if (pos = n) // This explanation is as follows {if (temp + fabs (x-sx) + fabs (y-sy)
     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.