Starting in the top left corner of a 2
2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20
20 grid?
Solution:
This can be solved with Dynamic programming, Brute force and, Combinatorics , I've used
combinatorics because it's the fastest way to do this.
Here's the java code for it:
public class Prob15 {
public static void main(String args[])
{
int gridSize = 20;
long path =1;
for(int i=0 ; i < gridSize ;i++)
{
path *= 2*gridSize-i;
path /= (i+1);
}
System.out.println(path);
}
}
No comments:
Post a Comment