-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp079.c
52 lines (50 loc) · 1.66 KB
/
p079.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
40
41
42
43
44
45
46
47
48
49
50
51
52
bool **mem;
bool f(const char **board, const int boardRowSize, const int boardColSize, const int i, const int j, const char *word)
{
if (word[0] == '\0') return true;
mem[i][j] = true;
if (i > 0 && word[0] == board[i-1][j] && !mem[i-1][j])
{
if (f(board, boardRowSize, boardColSize, i-1, j, word+1)) return true;
}
if (j > 0 && word[0] == board[i][j-1] && !mem[i][j-1])
{
if (f(board, boardRowSize, boardColSize, i, j-1, word+1)) return true;
}
if (i < boardRowSize-1 && word[0] == board[i+1][j] && !mem[i+1][j])
{
if (f(board, boardRowSize, boardColSize, i+1, j, word+1)) return true;
}
if (j < boardColSize-1 && word[0] == board[i][j+1] && !mem[i][j+1])
{
if (f(board, boardRowSize, boardColSize, i, j+1, word+1)) return true;
}
mem[i][j] = false;
return false;
}
bool exist(char** board, int boardRowSize, int boardColSize, char* word) {
if ((!boardRowSize) || (!boardColSize)) return false;
if ((!word) || word[0] == '\0') return true;
mem = (bool**)malloc(sizeof(bool*)*boardRowSize);
int i, j;
for (i = 0; i < boardRowSize; i++)
{
mem[i] = (bool*)malloc(sizeof(bool)*boardColSize);
for (j = 0; j < boardColSize; j++)
{
mem[i][j] = false;
}
}
for (i = 0; i < boardRowSize; i++)
for (j = 0; j < boardColSize; j++)
{
if (board[i][j] == word[0])
{
if (f(board, boardRowSize, boardColSize, i, j, word+1))
{
return true;
}
}
}
return false;
}