Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Recursion FAIL

Name: Anonymous 2010-02-08 22:29

I'm learning to hate recursion terribly, but I need to do it for this project.
The point of the project is to go from pointA to pointB using recursion in java. And print out all paths.
my input is
5
3 0
1 3

output should be about 10 paths in form of (col,row)
can I get a few pointers and maybe a little help?
private void pathFinder(int row, int col, int xB, int yB, int[][] array)
{
    if(row==xB && col==yB)
    {
        System.out.println("("+row+","+col+") ");
    }
    if(canIGoNorth(row,xB)==true)
    {
        System.out.print("("+row+","+col+") ");           
        pathFinder(row-1,col,xB,yB,array);
        System.out.print("("+row+","+col+") ");
    }
    if(canIGoEast(col,yB)==true)
    {
        System.out.print("("+row+","+col+") ");           
        pathFinder(row,col+1,xB,yB,array);
        System.out.print("("+row+","+col+") ");
           
    }
       
       
    }
inb4 learn to code, everythings wrong, and java sucks.

Name: Anonymous 2010-02-09 0:23

>>7
Try computing the Ackermann function, or traversing a tree/graph. You'll just end up emulating a stack or in the case of a tree/graph, you could avoid having a stack by keeping a sort of "next" pointer which allows you to traverse it sequentially in one way, but that just moves the cost of recursion from runtime to alloc time.
Those rules are fine for embedded programming, things like not using dynamic memory allocation(fixed pool of objects/memory?) are stupid on large systems as you'd eat into other processes memory and use more than you need at the moment. GC and dynamic allocation are perfectly fine for code meant for large systems (like our PCs).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List