Skip to content

Commit

Permalink
Unique path
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 7, 2017
1 parent 2e0af97 commit 4f193d6
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 062_unique_path/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test unique_path.c
57 changes: 57 additions & 0 deletions 062_unique_path/unique_path.c
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;
}
2 changes: 2 additions & 0 deletions 063_unique_paths_ii/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test unique_path.c
71 changes: 71 additions & 0 deletions 063_unique_paths_ii/unique_path.c
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;
}

0 comments on commit 4f193d6

Please sign in to comment.