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: Leo Ma <[email protected]>
- Loading branch information
1 parent
02922db
commit ba41477
Showing
2 changed files
with
68 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 word_search.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,66 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
static bool recursive(char *word, char **board, bool *used, int row, int col, int row_size, int col_size) | ||
{ | ||
if (board[row][col] != *word) { | ||
return false; | ||
} | ||
|
||
used[row * col_size + col] = true; | ||
|
||
if (*(word + 1) == '\0') { | ||
return true; | ||
} | ||
|
||
bool result = false; | ||
if (row > 0 && !used[(row - 1) * col_size + col]) { | ||
result = recursive(word + 1, board, used, row - 1, col, row_size, col_size); | ||
} | ||
|
||
if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col]) { | ||
result = recursive(word + 1, board, used, row + 1, col, row_size, col_size); | ||
} | ||
|
||
if (!result && col > 0 && !used[row * col_size + col - 1]) { | ||
result = recursive(word + 1, board, used, row, col - 1, row_size, col_size); | ||
} | ||
|
||
if (!result && col < col_size - 1 && !used[row * col_size + col + 1]) { | ||
result = recursive(word + 1, board, used, row, col + 1, row_size, col_size); | ||
} | ||
|
||
used[row * col_size + col] = false; | ||
return result; | ||
} | ||
|
||
static bool exist(char** board, int boardRowSize, int boardColSize, char* word) { | ||
int i, j; | ||
int len = strlen(word); | ||
if (len > boardRowSize * boardColSize) { | ||
return false; | ||
} | ||
bool *used = malloc(boardRowSize * boardColSize); | ||
for (i = 0; i < boardRowSize; i++) { | ||
for (j = 0; j < boardColSize; j++) { | ||
memset(used, false, boardRowSize * boardColSize); | ||
if (recursive(word, board, used, i, j, boardRowSize, boardColSize)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (argc < 3) { | ||
fprintf(stderr, "Usage: ./test word row1 row2...\n"); | ||
exit(-1); | ||
} | ||
printf("%s\n", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? "true" : "false" | ||
); | ||
return 0; | ||
} |