UVa 10068

From Algorithmist
Jump to: navigation, search

10068 - The Treasure Hunt[edit]

Summary[edit]

Help a treasure hunter find the best route to collect treasure. This is a nice two part problem, involving a Breadth-first search and then solving a small instance of the Traveling Salesperson Problem.

Explanation[edit]

We are given an R x C grid with R, C <= 20. The grid contains the locations of N <= 10 treasures, a starting location, an ending location and some blocks which cannot be passed through. We are given the amount of energy required for taking a single step when we are not holding any treasures. There are also N pickup and N carrying costs corresponding to the costs of picking a given treasure and of carrying it one unit on the grid.

We are to output a line containing the minimum amount of energy required to travel from the starting location to the ending location while making sure to pickup each treasure. We also have to one such path (any will do) which uses the minimum amount of energy. If there is no such path, we must output that the hunt is impossible.

Let an "interesting point" be a place on the grid that has a treasure or is a starting or ending location. The first step in solving this problem is to determine if there is any solution. There is a solution iff all the interesting points are connected. We can determine the connectivity using a single breadth first search from the starting node. If there is any interesting point which cannot be reached from the starting node, then the hunt is impossible. Otherwise, the hunt is possible. To make the problem of finding the best order to visit the treasures in, do a Breadth-first search from starting at each interesting point, and record the distance to each other interesting point in an (N+2) x (N+2) adjacency matrix.

After building the adjacency matrix, the problem is almost exactly a Traveling Salesperson Problem. The first thing to realize is that the pickup costs are virtually meaningless, any path that picks up every treasure will spend the same amount of pickup energy. The only difference between the treasure hunt problem and the travelling salesperson problem is the cost function; the structure of the computation remains unchanged. Instead of adding the cost of each edge that is taken along the optimal path, simply multiply the distance when taking an path between two interesting points by the accumulated weight of all the treasures in the visited set to get the cost of picking up the next treasure.

Input[edit]

2 2
ST
..
1
2 2
S#
#T
1
5 8
#......T
..#*..#.
..######
...*...#
####S.#*
5
10 50 50 100 30 80
10 10
#........*
..#*..#...
..######..
.......#..
####S..##.
.*.#...#..
.......#..
.##.#....#
.*.....#.#
....*..#.T
10
100 400 20 50 150 250 30 70 4 5
5 8
#......T
..#*..#.
..######
...**..#
####S.##
5
10 50 50 100 30 80
20 20
....................
.*..................
..........*.........
....................
....................
............*.......
....................
....................
..............*.....
.........S..........
....................
..............*.....
....................
....................
..*..........*......
....................
..........T.........
..*.................
....................
.....*..............
5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
20 20
....................
.*..................
..........*.........
....................
....................
............*.......
....................
....................
..............*.....
.........S..........
....................
..............*.....
....................
....................
..*..........*......
....................
..........T.........
..*.................
....................
.....*......*.......
5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 0

Output[edit]

 
Hunt #1
Minimum energy required = 1 cal
E

Hunt #2
The hunt is impossible.

Hunt #3
The hunt is impossible.

Hunt #4
Minimum energy required = 17539 cal
NWWWNNNEESPNWWSSSEEEESSSSSWSPNWWWPWNNNEPESEEEESEENNENNNNNPSSSSSWSSSSE

Hunt #5
Minimum energy required = 2835 cal
NPWPWWNNNEESPNEEEE

Hunt #6
Minimum energy required = 2453 cal
NNNNNNNNWWWWWWWWPEEEEEEEEESPEESSSPEESSSPSSSPWSSSPWWWWWWWWWWWPSSSPEEESSPNNNEEEEE

Hunt #7
Minimum energy required = 2952 cal
NNNNNNNNWWWWWWWWPEEEEEEEEESPEESSSPEESSSPSSSPWSSSPWWWWWWWWWWWPSSSPEEESSPEEEEEEEPNNNWW