-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path79. Word Search.c
78 lines (63 loc) · 1.9 KB
/
79. Word Search.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
79. Word Search
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
*/
const int offset[][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
bool validate(int r, int c, int *visited, char **board, int rowsz, int colsz, char *word) {
int i, j, k;
if (board[r][c] != *word) return false;
if (!*(word + 1)) return true;
visited[r * colsz + c] = 1;
for (k = 0; k < 4; k ++) {
i = r + offset[k][0];
j = c + offset[k][1];
if (i >= 0 && i <= rowsz - 1 &&
j >= 0 && j <= colsz - 1 &&
!visited[i * colsz + j]) {
if (validate(i, j, visited, board, rowsz, colsz, word + 1)) return true;
}
}
visited[r * colsz + c] = 0;
return false;
}
bool exist(char** board, int boardRowSize, int boardColSize, char* word) {
int i, j, *visited;
bool ret = false;
visited = calloc(boardRowSize * boardColSize, sizeof(int));
for (i = 0; !ret && i < boardRowSize; i ++) {
for (j = 0; !ret && j < boardColSize; j ++) {
if (validate(i, j, visited, board, boardRowSize, boardColSize, word)) ret = true;
}
}
free(visited);
return ret;
}
/*
Difficulty:Medium
Total Accepted:133.5K
Total Submissions:499.2K
Companies Microsoft Bloomberg Facebook
Related Topics Array Backtracking
Similar Questions
Word Search II
*/