-
Notifications
You must be signed in to change notification settings - Fork 0
/
79-word-search.c
126 lines (109 loc) · 3.74 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <stdlib.h>
typedef enum { false, true } bool;
bool has_left(int totalRow, int totalCol, int curRow, int curCol) {
if (curCol > 0) {
return true;
}
return false;
}
bool has_up(int totalRow, int totalCol, int curRow, int curCol) {
if (curRow > 0) {
return true;
}
return false;
}
bool has_right(int totalRow, int totalCol, int curRow, int curCol) {
if (curCol < totalCol-1) {
return true;
}
return false;
}
bool has_down(int totalRow, int totalCol, int curRow, int curCol) {
if (curRow < totalRow-1) {
return true;
}
return false;
}
bool search_from_pos(char **borad,
int boardRowSize,
int boardColSize,
int curRow,
int curCol,
char *word,
bool **matchMarkPtr) {
if (*word == '\0') {
return true;
}
if (borad[curRow][curCol] == *word && matchMarkPtr[curRow][curCol] != true) {
// 这里要特殊处理边界,不优雅
if (boardRowSize == 1 && boardColSize == 1 && *(word+1) == '\0') {
return true;
}
matchMarkPtr[curRow][curCol] = true;
if (has_up(boardRowSize, boardColSize, curRow, curCol) == true && \
search_from_pos(borad, boardRowSize, boardColSize, curRow-1, curCol, word+1, matchMarkPtr) == true) {
return true;
}
if (has_right(boardRowSize, boardColSize, curRow, curCol) == true && \
search_from_pos(borad, boardRowSize, boardColSize, curRow, curCol+1, word+1, matchMarkPtr) == true) {
return true;
}
if (has_down(boardRowSize, boardColSize, curRow, curCol) == true && \
search_from_pos(borad, boardRowSize, boardColSize, curRow+1, curCol, word+1, matchMarkPtr) == true) {
return true;
}
if (has_left(boardRowSize, boardColSize, curRow, curCol) == true && \
search_from_pos(borad, boardRowSize, boardColSize, curRow, curCol-1, word+1, matchMarkPtr) == true) {
return true;
}
matchMarkPtr[curRow][curCol] = false;
}
return false;
}
bool exist(char** board, int boardRowSize, int boardColSize, char* word) {
int curRow = 0;
int curCol = 0;
bool *matchMark = (bool *)calloc(boardRowSize*boardColSize, sizeof(bool));
bool **matchMarkPtr = (bool **)calloc(boardRowSize, sizeof(bool *));
for (int i = 0; i < boardRowSize; i++) {
// 注意这里的两种类型的转换
matchMarkPtr[i] = &matchMark[i*boardColSize];
}
for (int i = 0; i < boardRowSize; ++i) {
for (int j = 0; j < boardColSize; ++j) {
if (search_from_pos(board, boardRowSize, boardColSize, curRow, curCol, word, matchMarkPtr) == true) {
return true;
}
}
}
free(matchMark);
free(matchMarkPtr);
return false;
}
int main() {
/* char b1[] = {'A','B','C','E'}; */
/* char b2[] = {'S','F','C','S'}; */
/* char b3[] = {'A','D','E','E'}; */
/* char *board[3] = {b1,b2,b3}; */
// 指向指针的指针和多维数据是类型不兼容的
char b1[] = {'A'};
char *board[1] = {b1};
char *key;
bool result;
/* key = "ABCCED"; */
/* result = exist(board, 3, 4, key); */
/* printf("%s %d\n", key, result); */
/* key = "SEE"; */
/* result = exist(board, 3, 4, key); */
/* printf("%s %d\n", key, result); */
/* key = "ABCB"; */
/* result = exist(board, 3, 4, key); */
/* printf("%s %d\n", key, result); */
key = "A";
result = exist(board, 1, 1, key);
printf("%s %d\n", key, result);
key = "AB";
result = exist(board, 1, 1, key);
printf("%s %d\n", key, result);
}