-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion42.c
73 lines (55 loc) · 1.16 KB
/
question42.c
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
/*
http://practice.geeksforgeeks.org/problems/minimum-points-to-reach-destination/0
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
int min(int a, int b){
return a<b?a:b;
}
int max(int a, int b){
return a>b?a:b;
}
int findPoints(int rows, int cols, int arr[rows][cols]){
int dp[rows][cols];
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
dp[i][j] = 1;
}
}
dp[rows-1][cols-1] = (arr[rows-1][cols-1] > 0)?1: abs(arr[rows-1][cols-1]) + 1;
// printf("total is ...%d \n", total);
for(i=rows-2;i>=0;i--){
dp[i][cols-1] = max(dp[i+1][cols-1] - arr[i][cols-1], 1);
}
for(j=cols-2;j>=0;j--){
dp[rows-1][j] = max(dp[rows-1][j+1] - arr[rows-1][j], 1);
}
for(i=rows-2;i>=0;i--){
for(j=cols-2;j>=0;j--){
int minpts = min(dp[i][j+1],dp[i+1][j]);
dp[i][j] = max(minpts - arr[i][j],1);
}
}
return dp[0][0];
}
int main(){
int cases;
scanf("%d",&cases);
int i;
for(i=0;i<cases;i++){
int rows, cols;
scanf("%d %d", &rows, &cols);
int arr[rows][cols];
int k,l;
for(k=0;k<rows;k++){
for(l=0;l<cols;l++){
scanf("%d", &arr[k][l]);
}
}
printf("%d\n", findPoints(rows, cols, arr));
}
return 0;
}