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

Page 1 of 1

1 Replies - 705 Views - Last Post: 12 April 2014 - 10:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=344600&amp;s=1d8b418124b3a4d5e03cf00f00af55c8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 cs95

Reputation: 0
• Posts: 3
• Joined: 12-April 14

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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }