forked from Revnth/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Magic Grid.java
131 lines (95 loc) · 3.46 KB
/
Magic Grid.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Question - Magic Grid
You are given a magic grid A with R rows and C columns. In the cells of the grid, you will either get magic juice, which increases your strength by |A[i][j]| points, or poison, which takes away |A[i][j]| strength points. If at any point of the journey, the strength points become less than or equal to zero, then you will die.
You have to start from the top-left corner cell (1,1) and reach at the bottom-right corner cell (R,C). From a cell (i,j), you can only move either one cell down or right i.e., to cell (i+1,j) or cell (i,j+1) and you can not move outside the magic grid. You have to find the minimum number of strength points with which you will be able to reach the destination cell.
Input format:
The first line contains the number of test cases T. T cases follow. Each test case consists of R C in the first line followed by the description of the grid in R lines, each containing C integers. Rows are numbered 1 to R from top to bottom and columns are numbered 1 to C from left to right. Cells with A[i][j] < 0 contain poison, others contain magic juice.
Output format:
Output T lines, one for each case containing the minimum strength you should start with from the cell (1,1) to have a positive strength through out his journey to the cell (R,C).
Constraints:
1 ≤ T ≤ 5
2 ≤ R, C ≤ 500
-10^3 ≤ A[i][j] ≤ 10^3
A[1][1] = A[R][C] = 0
Time Limit: 1 second
Sample Input 1:
3
2 3
0 1 -3
1 -2 0
2 2
0 1
2 0
3 4
0 -2 -3 1
-1 4 0 -2
1 -2 -3 0
Sample Output 1:
2
1
2
Main code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Runner {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static int[][] takeInput() throws IOException {
String[] nm;
nm = br.readLine().split("\\s");
int n = Integer.parseInt(nm[0]);
int m = Integer.parseInt(nm[1]);
int arr[][]=new int[n][m];
if (n == 0) {
return arr;
}
String[] strNums;
for (int i = 0; i < n; ++i) {
strNums = br.readLine().split("\\s");
for (int j = 0; j < m; ++j){
arr[i][j] = Integer.parseInt(strNums[j]);
}
}
return arr;
}
public static void main(String[] args) throws IOException {
int t = Integer.parseInt(br.readLine().trim());
while (t!=0){
int[][] grid = takeInput();
System.out.println(Solution.getMinimumStrength(grid));
t--;
}
}
}
*/
// Solution:
public class Solution{
public static int getMinimumStrength(int[][] grid) {
int row=grid.length;
if (row==0)
return row;
int col=grid[0].length;
if (col==0)
return col;
int[][] dp=new int[row][col];
dp[row-1][col-1]=1;
for (int i=col-2;i>=0;i--)
{
dp[row-1][i]=dp[row-1][i+1]-grid[row-1][i];
}
for (int i=row-2;i>=0;i--)
{
dp[i][col-1]=dp[i+1][col-1]-grid[i][col-1];
}
for(int i=row-2;i>=0;i--)
{
for (int j=col-2;j>=0;j--)
{
int ans1=dp[i+1][j];
int ans2=dp[i][j+1];
dp[i][j]=Math.max(1,Math.min(ans1,ans2)-grid[i][j]);
}
}
return dp[0][0];
}
}