c++ - Breadth First Search using array -
i trying implement breadth first search maze using arrays. problem i'm facing cant expand paths @ same time. this:
####### 123456# #3###7# #4###8# #5###9g #######
the method i'm using using loop loop through array. set starting point c (current) , end point g (goal). when iterate through maze, search array location store character 'c'. checked if up, down, left, right wall. if not, i'll moved next bracket. new bracket set 'c' , previous bracket set 'o'.
for (int = 1; < 12; i++) { (int j = 0; j < 12; j++) { // find current node if (maze[i][j] == 'c') { if (maze[i][j-1] == ' ') // { maze[i][j-1] = 'c'; maze[i][j] = 'o'; } else if (maze[i][j+1] == ' ') // down { maze[i][j+1] = 'c'; maze[i][j] = 'o'; } else if (maze[i-1][j] == ' ') // left { maze[i-1][j] = 'c'; maze[i][j] = 'o'; } else if (maze[i+1][j] == ' ') { maze[i+1][j] = 'c'; maze[i][j] = 'o'; } }
how implement part 'c' bracket @ junction? using loop keep coming out this:
####### ####### ####### ####### ####### ####### ####### ####### c # oc # ooc # oooc # ooooc # oooooc# oooooo# oooooo# # ### # # ### # #c### # #c### # #c### # #c### # #o###c# #o###o# # ### # # ### # # ### # # ### # # ### # # ### # #c### # #o###c# # ### # ### # ### # ### # ### # ### # ### #c### ####### ####### ####### ####### ####### ####### ####### #######
in general, don't literally expand paths simultaneously in bfs. instead, put first element queue, , iteratively pop first element front , add elements of queue reachable element , not contained in queue already.
you trying work on map only; not possible bfs. need queue contains positions of visited elements , way mark elements done.
http://en.wikipedia.org/wiki/breadth-first_search has nice example that.
Comments
Post a Comment