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 Room
s and a Room
can contain Object
s. Each Room
has attributes indicating which other Room
s can be reached from there (north/east/west/south).
Eg possible directions from room 3 :
room 1 (east)
room 4 (south)
There is also a seperate list of Object
s, let's call them targets .
The goal is to "collect" all targets , meaning create a path through the Room
s 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.
Here's a crude approach:
The path formed by joining all the segments will satisfy your requirements, as I understand them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.