0

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.

3
  • Are you trying to find the shortest such path, or will just any path do? Commented Jul 29, 2016 at 22:13
  • Any path will do and order of which the targets are collected does not matter. Commented Jul 29, 2016 at 22:16
  • 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. Commented Jul 29, 2016 at 22:16

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.