recursive problem?
hey guys,
This is my first post here. I don't know if this is the right place to ask these kinds of questions. But in any case i go ahead and ask:
I have a 2 dimension 8 by 8 array which is fill with "."s and 2 Xs. I need to locate the 2Xs and then find the shortest possible path from one X to another X recursivley. Can anyone help me to find the solution please? Thanks in advance. Here is the array which i've:
char grid[][] = {
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','X','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','X','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'}
};
[1546 byte] By [
jami] at [2007-11-11 7:12:51]

# 1 Re: recursive problem?
are you sure only "." and "X" values are used ?
are there not any blocks ?
becos your shortest path is very simple now
no need recursion.
mr1yh1 at 2007-11-11 22:39:00 >

# 2 Re: recursive problem?
are you sure only "." and "X" values are used ?
are there not any blocks ?
becos your shortest path is very simple now
no need recursion.
yes, sorry there's block between them
char grid[][] = {
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','X','.','A','.','.','.'},
{'.','.','.','.','A','X','.','.'},
{'.','.','.','.','A','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'}
};
Finding the shortest possible path between 2 Xs is complicated stuff but i really don't know how I would first locate the 2 Xs positions recursivley?
jami at 2007-11-11 22:40:00 >

# 3 Re: recursive problem?
-you can simply find 'X' using "for loops" no need recursion.
later you may use recursive code for shortest path.
-once i wrote a non-recursive one. ( message #6 )
http://forum.ceviz.net/showthread.php?t=23642
look iterate() and findPathFor(...) functions.
another solution link in message #7
( run this program to see how both programs mark cells
using distance, it has a nice GUI )
iterate() function is simple, it looks at the "right , left, up and down" cells of every cell.
if distance of neighbour is bigger than "current cell's distance + 1"
or non calculated before ( zero forexample ),
it become "current cell's distance + 1" ...
its possible to write a similar recursive function
( but why recursive ? recursive will crush if you search in a big matrix ).
mr1yh1 at 2007-11-11 22:41:04 >

# 4 Re: recursive problem?
-you can simply find 'X' using "for loops" no need recursion.
later you may use recursive code for shortest path.
-once i wrote a non-recursive one. ( message #6 )
http://forum.ceviz.net/showthread.php?t=23642
look iterate() and findPathFor(...) functions.
another solution link in message #7
( run this program to see how both programs mark cells
using distance, it has a nice GUI )
iterate() function is simple, it looks at the "right , left, up and down" cells of every cell.
if distance of neighbour is bigger than "current cell's distance + 1"
or non calculated before ( zero forexample ),
it become "current cell's distance + 1" ...
its possible to write a similar recursive function
Thank you very much, it was very helpful and kind of you providing an example code. I'll try to write that algorithm recursively.
( but why recursive ? recursive will crush if you search in a big matrix ).
I don't know my teacher want me to do it recursively. I even had a big arugement other day in other forum on recursion. It's just a big while loop with a new stack frame every iteration. It will never be necessary. Although there're certain problems which can only be solved by recursion efficient algorithms, I think for big programs looping is better than recursion.
Anyway, once again thanks for the help.
Regards,
M.Salman
jami at 2007-11-11 22:42:03 >

# 5 Re: recursive problem?
What language does this need to done in Jami.. I noticed you also posted in cpp-home forums??
http://www.cpp-home.com/forum/viewtopic.php?t=11797
# 6 Re: recursive problem?
@jami
that problem's real domain is "graph data structure"
( in a more complicated way ). i saw they use a Stack or Queue for translating recursive logic into loops.
its possible to translate a recursive algorithm :
forexample searching in a tree non-recursive way.
-put first Node into Queue.
-while Queue is not empty ,
--pop() a Node
--check, if its the one you search. ( if its , job finished )
--put all child nodes of that node into Queue.
-
functional programming languages such as lisp, schema
(and programmers in that camp) are good in recursive programming.
but other languages ( and programmers ) dont feel comfort with it.
mr1yh1 at 2007-11-11 22:44:06 >

# 7 Re: recursive problem?
What language does this need to done in Jami.. I noticed you also posted in cpp-home forums??
http://www.cpp-home.com/forum/viewtopic.php?t=11797
;) In java; actually few days before i wasn't aware of this forum so i posted there to get some ideas. And you know C++ is quite similar to java. I don't want the code, need some puesdocode help.
jami at 2007-11-11 22:45:04 >

# 8 Re: recursive problem?
You could use the A* (A-star) algorithm. Though a bit more complex than a few loops. Here's what I think is the best tutorial out there for this:
A-star pathfinding ( http://www.gamedev.net/reference/articles/article2003.asp)