My problem consists of finding a path from a given start to a given finish cell in a 2-dimensional array filled with obstacles (cells that I cannot access). While I think that I have found an algorithm that finds the optimal path each time given that the obstacle cells are known, the thing is that those obstacle cells are not specified beforehand and must be randomly generated each time I'm starting the program.
I'm stuck trying to find an algorithm that pseudo-randomly generates those obstacles while ensuring that each time a path from start to finish is indeed available. Anybody got any ideas? Thanks in advance.
This is what I've thought of so far for the obstacles (python):
free=N*N-4*(N-1)
obstacles=round(free*p)
#for j in (1, N):
obstacle_free_points = {S, F}
count=0
#j = j + 1
j=self.prims(N,S,F)
while count<obstacles:
x=randrange(1,N-1)
y=randrange(1,N-1)
if (x,y) not in obstacle_free_points or j:
self.grid[x][y]=1
count=count+1
And this is my BFS for finding the path afterwards (python):
def bfs(self,N,S,F):
previous=np.zeros((N, N),dtype=object)
seen=set()
q=Queue()
q.put(S)
while not q.empty():
node=q.get()
if node==F:
path=[F]
while node!=S:
path.append(previous[node[0]][node[1]])
node=previous[node[0]][node[1]]
return path
for n in grid.adjacent(self,node):
if n not in seen:
q.put(n)
previous[n[0]][n[1]]=node
seen.add(node)