-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy path63.c
39 lines (38 loc) · 1.23 KB
/
63.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
/*
I walk through the grids and record the path numbers at the
same time.
By using a 2D array called paths, it will add up possible so
urce path and save the number.
Noted that:
if the destination has obstacle, we can't reach it
the first grid (paths[0][0]) always set as 1 our previous
path source is either from top or left border of grid has
different condition
*/
int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize,
int* obstacleGridColSize)
{
if (obstacleGrid[obstacleGridSize - 1][*obstacleGridColSize - 1] == 1)
{
return 0;
}
int paths[obstacleGridSize][*obstacleGridColSize];
for (int i = 0; i < obstacleGridSize; i++)
{
for (int j = 0; j < *obstacleGridColSize; j++)
{
if (obstacleGrid[i][j])
{
paths[i][j] = 0;
}
else
{
paths[i][j] = (i == 0 && j == 0)
? 1
: ((i == 0 ? 0 : paths[i - 1][j]) +
(j == 0 ? 0 : paths[i][j - 1]));
}
}
}
return paths[obstacleGridSize - 1][*obstacleGridColSize - 1];
}