-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path64.c
45 lines (40 loc) · 1.19 KB
/
64.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
#include <stdio.h>
#include <stdlib.h>
int minPathSum(int **grid, int nRows, int nCols) {
int i, j;
for (i = 0; i < nRows; i++) {
for (j = 0; j < nCols; j++) {
if (i > 0 && j == 0)
grid[i][j] += grid[i - 1][j];
if (j > 0 && i == 0)
grid[i][j] += grid[i][j - 1];
if (i > 0 && j > 0) {
if (grid[i][j - 1] < grid[i - 1][j])
grid[i][j] += grid[i][j - 1];
else
grid[i][j] += grid[i - 1][j];
}
}
}
/*
for (i = 0; i < nRows; i++) {
for (j = 0; j < nCols; j++) {
printf("%d ", grid[i][j]);
}
printf("\n");
}
*/
return grid[nRows - 1][nCols - 1];
}
int main() {
int m, n, i;
m = n = 3;
int **grid = (int **) calloc (m, sizeof(int *));
for (i = 0; i < m; i++)
grid[i] = (int *) calloc (n, sizeof(int));
grid[0][0] = 1; grid[0][1] = 2; grid[0][2] = 3;
grid[1][0] = 1; grid[1][1] = 2; grid[1][2] = 3;
grid[2][0] = 1; grid[2][1] = 2; grid[2][2] = 3;
printf("%d\n", minPathSum(grid, m, n));
return 0;
}