24h購物| | PChome| 登入
2012-01-29 16:52:06| 人氣1,241| 回應0 | 上一篇 | 下一篇

[UVA] 10496 - Collecting Beepers [TSP][DP]

推薦 0 收藏 0 轉貼0 訂閱站台

Problem E
Collecting Beepers
Input: standard input
Output:
standard output
Time Limit:
2 seconds

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 + 11), (i, j-1 ), (i-1, j), or (i+1, j), j) has a cost of one.

 

You can assume that Karel’s world is never larger than 20 x _ 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



作法 : 求一個走過全部的點, 又回到起始點的最短路徑長


#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#define maxState 1<<12
#define maxN 12
int DP[maxState][maxN], map[20][20];
int TSP(int state, int last, int n) {
    if(state == 0 && last != 0)    return 0xfffff;
    if(DP[state][last] != -1)    return DP[state][last];
    int i, min = 0xfffff, tmp;
    for(i = 0; i <= n; i++) {
        if((state&(1<<i)) != 0) {
            tmp = TSP(state-(1<<i), i, n);
            tmp += map[i][last];
            if(min > tmp)    min = tmp;
        }
    }
    DP[state][last] = min;
    return DP[state][last];
}
int main() {
    int T, x[20], y[20];
    int sizex, sizey, n, i, j;
    scanf("%d", &T);
    while(T--) {
        scanf("%d %d", &sizex, &sizey);
        scanf("%d %d", &x[0], &y[0]);
        scanf("%d", &n);
        for(i = 1; i <= n; i++)
            scanf("%d %d", &x[i], &y[i]);
        for(i = 0; i <= n; i++)
            for(j = 0; j <= n; j++)
                map[i][j] = abs(x[i]-x[j])+abs(y[i]-y[j]);
        memset(DP, -1, sizeof(DP));
        DP[0][0] = 0;
        printf("The shortest path has length %d\n", TSP((1<<(n+1))-1, 0, n));
    }
    return 0;
}

台長: Morris
人氣(1,241) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 11284 - Shopping Trip
此分類上一篇:[UVA] 471 - Magic Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文