-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgrid.java
More file actions
48 lines (38 loc) · 1.08 KB
/
grid.java
File metadata and controls
48 lines (38 loc) · 1.08 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
// Traverse from top left to bottom right of 2d array m X n
// from each cell u can go to the cell on right or down
// return the maximum sum possible
import java.util.*;
public class problem
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int X,Y; //X:number of rows, Y: number of columns(X=Y) because square
System.out.println("Enter size");
X=sc.nextInt();
Y=X;
int[][] Cost= new int[X][Y];
for(int i=0;i<X;i++)
{
for(int j=0;j<Y;j++)
{
//Take input the cost of visiting cell (i,j)
Cost[i][j]=sc.nextInt();
}
}
int[][] MaxCost= new int[X][Y];
MaxCost[0][0] = Cost[0][0];
for(int j=1;j<Y;j++)
MaxCost[0][j] = MaxCost[0][j-1] + Cost[0][j];
for(int i=1;i<X;i++)
MaxCost[i][0] = MaxCost[i-1][0] + Cost[i][0];
for(int i=1;i<X;i++)
{
for(int j=1;j<Y;j++)
{
MaxCost[i][j] = Math.max(MaxCost[i-1][j],MaxCost[i][j-1]) + Cost[i][j];
}
}
System.out.println(MaxCost[X-1][Y-1]);
}
}