Maze Traveller Solution in Java solved using recursion.
import java.util.*;
class mazeTraveller
{
static HashMap<String, Long> map = new HashMap<>();
static long calculate(int row , int col) {
return _calculate(row, col , map);
}
static long _calculate(int row ,int col,HashMap<String,Long> map) {
String key = row+":"+col;
if(map.containsKey(key)) return map.get(key);
if(row == 0 || col == 0 ) return 0;
if(col == 1 || row == 1) return 1;
long res = _calculate(row-1,col,map) + _calculate(row,col-1,map);
map.put(key,res);
return map.get(key);
}
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
try
{
int row = input.nextInt();
int col = input.nextInt();
long paths = calculate(row,col);
System.out.println("There are "+paths + " to travel in maze");
}
catch(Exception e){
return;
}
}
}
5 5
There are 70 to travel in maze
18 18
There are 2333606220 to travel in maze
2 10
There are 10 to travel in maze