Skip to content

Commit 4f4a54f

Browse files
添加 0063.不同路径II.md C语言版本
1 parent e6fccc5 commit 4f4a54f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

problems/0063.不同路径II.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,60 @@ var uniquePathsWithObstacles = function(obstacleGrid) {
333333
};
334334
```
335335

336+
C
337+
```c
338+
//初始化dp数组
339+
int **initDP(int m, int n, int** obstacleGrid) {
340+
int **dp = (int**)malloc(sizeof(int*) * m);
341+
int i, j;
342+
//初始化每一行数组
343+
for(i = 0; i < m; ++i) {
344+
dp[i] = (int*)malloc(sizeof(int) * n);
345+
}
346+
347+
//先将第一行第一列设为0
348+
for(i = 0; i < m; ++i) {
349+
dp[i][0] = 0;
350+
}
351+
for(j = 0; j < n; ++j) {
352+
dp[0][j] = 0;
353+
}
354+
355+
//若碰到障碍,之后的都走不了。退出循环
356+
for(i = 0; i < m; ++i) {
357+
if(obstacleGrid[i][0]) {
358+
break;
359+
}
360+
dp[i][0] = 1;
361+
}
362+
for(j = 0; j < n; ++j) {
363+
if(obstacleGrid[0][j])
364+
break;
365+
dp[0][j] = 1;
366+
}
367+
return dp;
368+
}
369+
370+
int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obstacleGridColSize){
371+
int m = obstacleGridSize, n = *obstacleGridColSize;
372+
//初始化dp数组
373+
int **dp = initDP(m, n, obstacleGrid);
374+
375+
int i, j;
376+
for(i = 1; i < m; ++i) {
377+
for(j = 1; j < n; ++j) {
378+
//若当前i,j位置有障碍
379+
if(obstacleGrid[i][j])
380+
//路线不同
381+
dp[i][j] = 0;
382+
else
383+
dp[i][j] = dp[i-1][j] + dp[i][j-1];
384+
}
385+
}
386+
//返回最后终点的路径个数
387+
return dp[m-1][n-1];
388+
}
389+
```
336390
337391
-----------------------
338392
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
 (0)