nels 105000.80 mary 20000.50 frank 18153.33 nancy 91868.73 michelle 20858Use a Scanner object, not a Console object, to read input because the TAs may redirect stdin in order to test your program. You may assume that the input is error free.
average salary = 51,176.27 nels 105,000.80 true mary 20,000.50 false frank 18,153.33 false nancy 91,868.73 true michelle 20,858.00 false
Pet Detective is a game published by Lumosity that asks you to deliver pets to their homes within a specified number of moves. A screen shot of Pet Detective is shown below and you can view a video of a person playing the game here. Please note that you can skip over a pet without picking it up or skip over a home without visiting it.
As you can see from the video, at each turn you move the car to either a pet, thus picking it up, or to a pet home, thus dropping it off. As you can also observe, the car has a capacity of 4 pets. Hence once you've loaded 4 pets into the car, you must deliver a pet to its home at the next turn before you can pick up another pet.
This problem can be difficult to solve at the upper levels and one might want to write a program to try to find a solution to the puzzle (especially since Lumosity occasionally uses a puzzle that can't be solved in the required number of moves).
Solving Strategy. These puzzles can be expressed as an optimization problem. The puzzle solution is a Hamiltonian Circuit and hence the problem is NP-complete. Duane Mattern has managed to express this problem as an integer programming problem. However, a more efficient way to solve this problem is as a graph problem since you can take advantage of knowing the distances between nodes. You must solve this problem as a graph problem using the following formulation. You should represent the car, each pet, and each pet home as a vertex. You will then need to use a brute force, recursive search algorithm to find a solution and this search might take several minutes.
You are free to investigate any search algorithm you want, but my solution involved always moving the car to the nearest unsearched vertex, subject to the constraint that if the car was full, the nearest vertex had to be a pet home, and then recursively continuing the search from that vertex. I would backtrack if the search failed to find a solution.
Floyd Warshall Algorithm. You will need to use the Floyd Warshall algorithm presented in class to compute the shortest path between each pair of vertices before you start your exhaustive search.
Input. The input will be a file whose name is given as a command line argument. The first line of your input will be the number of moves you can use to deliver your pets. The remaining lines of input will consist of vertex pairs and the distance between the pairs. For the above screen shot, the input might begin:
29 car marmot 1 car armadillo 1 fox_home cat_home 1 fox_home yellow_dog 1 cat_home marmot_home 1 cat_home yellow_dog_home 2 ...I make the following guarantees about the input:
If the problem has a solution, then print out the vertices in the order in which they get visited. For example, if the car in the above screenshot makes it first moves to the armadillo, then the cardinal, then the cat, and then the armadillo home, then your output would start as:
armadillo cardinal cat armadillo_home ...
If the puzzle cannot be solved in the given amount of moves, then print "No solution found".
Data Structure Requirements: This is a homework assignment using Java's collection classes, so you must use the following data structures:
You will probably need to use an array or ArrayList to handle your adjacency matrix. Since you don't know in advance either the number of pets or the maximum number of pets, it's probably easier to use an ArrayList. You should feel free to use other Java data structures as necessary.
java -jar petdet.jar puzzleFile
./petdet_checker puzzleFile < puzzleSolution
Please submit the following two files: