Page 1 of 1

# How to list all possible paths from point A to B by using recursion?

Posted 12 April 2014 - 10:19 AM

Hi this is my first post ever so I'll try my best to make it clear as I can. From a two-dimensional grid 5x5 that looks like this:

(0,0)(0,1)(0,2)(0,3)(0,4)
(1,0)(1,1)(1,2)(1,3)(1,4)
(2,0)(2,1)(2,2)(2,3)(2,4)
(3,0)(3,1)(3,2)(3,3)(3,4)
(4,0)(4,1)(4,2)(4,3)(4,4)

We have Starting point that is (3,0) and an ending point is (1,3). We can only move up and right to get to the ending point by using recursion. We have to list all possible paths from (3,0) to (1,3)

Example: paths:(3,0)(2,0)(1,0)(1,1)(1,2)(1,3)
(3,0)(2,0)(2,1)(1,1)(1,2)(1,3)
etc...

I was able to get from (3,0) to (1,3) but I have no idea on how to list the other paths. This is my code so far
```public class Program7 {
public static void main(String[] args){

int size = 5;

int x1 = 3;
int y1 = 0;
int x2 = 1;
int y2 = 3;

System.out.println(x1+" "+y1);
System.out.println(x2+" "+y2);

int [][] path = new int[size][size];
grid(path,x1,y1,x2,y2);
}

public static int grid(int[][] path,int x1, int y1, int x2, int y2){
path[x1][y1] = 1;
path[x2][y2] = 2;

System.out.print("("+x1+","+y1+")");
if(path[x1][y1] == path[x2][y2])
return -1;
else if(x1>x2){
return grid(path,x1-1,y1,x2,y2);
}
else if(y1<y2){
return grid(path,x1,y1+1,x2,y2);
}
else return -1;
}

}
```

Is This A Good Question/Topic? 0

## Replies To: How to list all possible paths from point A to B by using recursion?

### #2 macosxnerd101 • • Games, Graphs, and Auctions
•     Reputation: 12742
• Posts: 45,926
• Joined: 27-December 08

## Re: How to list all possible paths from point A to B by using recursion?

Posted 12 April 2014 - 10:20 AM