0
votes

I have three Java model classes: Map, Room, Object. These are mapped from the following XML file using JAXB:

<map>
   <room id="1" name="Stairway" east="2"/>
   <room id="2" name="Kitchen" north="4" south="3" west="1">
       <object name="Hat"/>
   </room>
   <room id="3" name="Hallway" east="1" south="4">
       <object name="Money"/>
   </room>
   <room id="4" name="Bathroom" south="2"/>
</map>

A Map contains Rooms and a Room can contain Objects. Each Room has attributes indicating which other Rooms can be reached from there (north/east/west/south).
E.g. possible directions from room 3:
room 1 (east)
room 4 (south)

There is also a seperate list of Objects, let's call them targets.

The goal is to "collect" all targets, meaning create a path through the Rooms which contain the targets.
You can think of it as a graph (Map) with nodes (Room) and edges (directions north/east/west/south that lead to another Room).

public Route findRoute(Map map, Room startRoom, List<Object> targets) {
        // walk through the map, find all targets 
        // once found them all return the route
}

So basically I am looking for a clean way/algorithm to create a path through that graph where each node contains an Object from my target list.

I know the Dijkstra algorithm but I think it does not fit my use case as I have a condition that must be met (a Room must contain a specific Object). There are few other path finding algorithms, but I couldn't figure out a solution for my particular problem.

Any help appreciated.

EDIT:

Any path will do (shortest path is a nice-to-have, but not mandatory) and the order of which the targets are collected does not matter.

1
Are you trying to find the shortest such path, or will just any path do? - John Bollinger
Any path will do and order of which the targets are collected does not matter. - lenny
Dijkstra's algorithm is not well suited because it will not find paths containing loops, and you may need such a path -- for example, if there is an object in a room with only one entrance / exit. - John Bollinger

1 Answers

0
votes

Here's a crude approach:

  1. Let the entrance be the initial starting point.
  2. Select one target not yet reached.
  3. Perform a breadth-first (or depth first) search to locate a path to that target, marking as well any other targets you encounter along that path.
  4. If all targets have been located then stop.
  5. Let the room where the selected target was located be the next starting point.
  6. Goto (2).

The path formed by joining all the segments will satisfy your requirements, as I understand them.