-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmazePath.java
More file actions
56 lines (55 loc) · 1.2 KB
/
mazePath.java
File metadata and controls
56 lines (55 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
public class mazePath
{
//recursive
// static int maze_path(int sr,int sc,int dr,int dc)
// {
// if(sr==dr&&sc==dc)
// return 1;
// if(sr>dr ||sc>dc)
// return 0;
// int cstod=0;
// int ch=maze_path(sr,sc+1,dr,dc);
// int cv=maze_path(sr+1,sc,dr,dc);
// cstod=ch+cv;
// return cstod;
// }
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// Scanner s=new Scanner(System.in);
// int sr=s.nextInt();
// int sc=s.nextInt();
// int dr=s.nextInt();
// int dc=s.nextInt();
// System.out.print(maze_path(sr,sc,dr,dc));
// }
// }
//bottom up approach | memoized
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int sr=s.nextInt();
int sc=s.nextInt();
int dr=s.nextInt();
int dc=s.nextInt();
int dp[][]=new int[dr+1][dc+1];
System.out.print(maze_path(sr,sc,dr,dc,dp));
}
public static int maze_path(int sr,int sc,int dr,int dc,int dp[][])
{
if(sr==dr && sc==dc)
return 1;
if(sr>dr ||sc>dc)
return 0;
if(dp[sr][sc]!=0)
{
return dp[sr][sc];
}
int cstod=0;
int ch=maze_path(sr,sc+1,dr,dc,dp);
int cv=maze_path(sr+1,sc,dr,dc,dp);
cstod=ch+cv;
dp[sr][sc]=cstod;
return cstod;
}
}