Pergunta

Iam trying to do a program which gives us shortest path on a map(map is a picture which has Rectangle on it. and my turn points are Rectangle's edge points. can anybody help me to find shortest path on this points?Red dots are my point which ı must find shortest path in it

As you can see ı have begin point and an end point. ımust start from begin to end and path must be from point. path can not be on Rectangle because it is my wall so ı cant go in or over it. So can any body help???

Foi útil?

Solução

If efficiency is not an issue and a simple solution is OK, you could consider the following pseudo-code:

visited = new bool[N,M]
points = new List<Point>()
prev = new List<int>() 
points.Add(Begin)
prev.Add(-1)
visited[Begin.X, Begin.Y] = true

for(i = 0; i < points.Length; i++)
    p = points(i)
    foreach neighbor of p
        if neighbor is not wall && !visited[neighbor.X, neighbor.Y]
            points.Add(neighbor)
            prev.Add(i)
            visited[neighbor.X, neighbor.Y] = true
            if neighbor == End
                 // we are done, print path (without Begin and End)
                 j = i
                 while j != 0
                     print points[j]
                     j = prev[j]
                 return
// no solution found

(It is just a modification of Flood fill algorithm, http://en.wikipedia.org/wiki/Flood_fill.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top