Calculate shorthest path in a maze

Hi,

Does anybody know how I can calculate the shortest route in a maze.

In my maze (2D array) walls are represented as a "2". There are monsters in it and they have to take the shortest path to my character without walking into walls.

How shoud I handle this?

Some sort of algorithm?

Greetz Daan

[333 byte] By [D-a-a-na] at [2007-11-27 4:20:16]
# 1
This sounds similar to another question from last week... (about calculating the distance between 2 points in a 2D array)
xiarcela at 2007-7-12 9:27:15 > top of Java-index,Java Essentials,Java Programming...
# 2

If you are going to do a maze and want to calculate shortest path, you should ditch arrays and look into graphs.

http://www.cs.sunysb.edu/~algorith/files/graph-data-structures-L.gif

each node and edge would have a variable or static weight on it. Depending on where your monster is and the location of the target is, you would do the necessary calculation, and go.

kdajania at 2007-7-12 9:27:15 > top of Java-index,Java Essentials,Java Programming...
# 3

@xiardana:

It's already possible to do that. I used the formula from this site

http://www.mathwarehouse.com/algebra/distance_formula/

But that's something different than finding the shortest path, the formula doesn't take walls in acount and I need something that calculates several routes so I can pick out the shortest route.

@kdajani

I don't quite understand i guess. I also can't reach the image, the site looks down.

Greetz Daan

D-a-a-na at 2007-7-12 9:27:15 > top of Java-index,Java Essentials,Java Programming...
# 4
Look up Djikstra's algorithm. It describes how to find the shortest path from one point on a graph to another. You can handle walls by only including "open" points in the array in the graph. http://en.wikipedia.org/wiki/Dijkstra's_algorithm
hunter9000a at 2007-7-12 9:27:15 > top of Java-index,Java Essentials,Java Programming...
# 5
The link still works for me. Djikstra's algorithm would give you full details on Graphs.
kdajania at 2007-7-12 9:27:15 > top of Java-index,Java Essentials,Java Programming...
# 6

You can treat your 2d array as a graph by making each element in the array a node, and creating an edge between each adjacent node. If you can only move hor. and vert. in the maze, then your edges will only be between node to the left/right and above/below. If you don't add wall elements to the graph, then they won't be considered as part of the path by the algorithm (they won't exist according as far as the graph is concerned, so they can't be used as part of the path.)

hunter9000a at 2007-7-12 9:27:15 > top of Java-index,Java Essentials,Java Programming...
# 7
> Look up Djikstra's algorithm. It describes how toor maybe A* http://en.wikipedia.org/wiki/A%2A_search
tjacobs01a at 2007-7-12 9:27:15 > top of Java-index,Java Essentials,Java Programming...