forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
2e0af97
commit 4f193d6
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test unique_path.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static void valid_path_recursive(int col, int m, int row, int n, int *count) | ||
{ | ||
if (col == m - 1 && row == n - 1) { | ||
(*count)++; | ||
} else { | ||
if (m > n) { | ||
if (col < m - 1) { | ||
valid_path(col + 1, m, row, n, count); | ||
} | ||
if (row < n - 1) { | ||
valid_path(col, m, row + 1, n, count); | ||
} | ||
} else { | ||
if (col < m - 1) { | ||
valid_path(col + 1, m, row, n, count); | ||
} | ||
if (row < n - 1) { | ||
valid_path(col, m, row + 1, n, count); | ||
} | ||
} | ||
} | ||
} | ||
|
||
static int uniquePaths(int m, int n) { | ||
//int count = 0; | ||
//valid_path(0, m, 0, n, &count); | ||
//return count; | ||
int row, col; | ||
int *grids = malloc(m * n * sizeof(int)); | ||
for (col = 0; col < m; col++) { | ||
grids[col] = 1; | ||
} | ||
for (row = 0; row < n; row++) { | ||
grids[row * m] = 1; | ||
} | ||
for (row = 1; row < n; row++) { | ||
for (col = 1; col < m; col++) { | ||
grids[row * m + col] = grids[row * m + col - 1] + grids[(row - 1) * m + col]; | ||
} | ||
} | ||
int result = grids[m * n - 1]; | ||
free(grids); | ||
return result; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc != 3) { | ||
fprintf(stderr, "Usage: ./test m n\n"); | ||
exit(-1); | ||
} | ||
printf("%d\n", uniquePaths(atoi(argv[1]), atoi(argv[2]))); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test unique_path.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridRowSize, int obstacleGridColSize) | ||
{ | ||
int row, col; | ||
int reset = 0; | ||
for (row = 0; row < obstacleGridRowSize; row++) { | ||
if (reset) { | ||
obstacleGrid[row][0] = 1; | ||
} else { | ||
if (obstacleGrid[row][0] == 1) { | ||
reset = 1; | ||
} | ||
} | ||
} | ||
|
||
reset = 0; | ||
for (col = 0; col < obstacleGridColSize; col++) { | ||
if (reset) { | ||
obstacleGrid[0][col] = 1; | ||
} else { | ||
if (obstacleGrid[0][col] == 1) { | ||
reset = 1; | ||
} | ||
} | ||
} | ||
|
||
for (row = 0; row < obstacleGridRowSize; row++) { | ||
int *line = obstacleGrid[row]; | ||
for (col = 0; col < obstacleGridColSize; col++) { | ||
line[col] ^= 1; | ||
} | ||
} | ||
|
||
for (row = 1; row < obstacleGridRowSize; row++) { | ||
int *last_line = obstacleGrid[row - 1]; | ||
int *line = obstacleGrid[row]; | ||
for (col = 1; col < obstacleGridColSize; col++) { | ||
if (line[col] != 0) { | ||
line[col] = line[col - 1] + last_line[col]; | ||
} | ||
} | ||
} | ||
|
||
return obstacleGrid[obstacleGridRowSize - 1][obstacleGridColSize - 1]; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc < 3) { | ||
fprintf(stderr, "Usage: ./test m n\n"); | ||
exit(-1); | ||
} | ||
|
||
int i, j, k = 3; | ||
int row_size = atoi(argv[1]); | ||
int col_size = atoi(argv[2]); | ||
int **grids = malloc(row_size * sizeof(int *)); | ||
for (i = 0; i < row_size; i++) { | ||
grids[i] = malloc(col_size * sizeof(int)); | ||
int *line = grids[i]; | ||
for (j = 0; j < col_size; j++) { | ||
line[j] = atoi(argv[k++]); | ||
printf("%d ", line[j]); | ||
} | ||
printf("\n"); | ||
} | ||
printf("%d\n", uniquePathsWithObstacles(grids, row_size, col_size)); | ||
return 0; | ||
} |