-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordsearch.c
More file actions
218 lines (182 loc) · 6 KB
/
Copy pathwordsearch.c
File metadata and controls
218 lines (182 loc) · 6 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Declarations of the two functions you will implement
// Feel free to declare any helper functions or global variables
void printPuzzle(char **arr);
void searchPuzzle(char **arr, char *word);
int bSize;
int size = 8;
// Main function, DO NOT MODIFY
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <puzzle file name>\n", argv[0]);
return 2;
}
int i, j;
FILE *fptr;
// Open file for reading puzzle
fptr = fopen(argv[1], "r");
if (fptr == NULL) {
printf("Cannot Open Puzzle File!\n");
return 0;
}
// Read the size of the puzzle block
fscanf(fptr, "%d\n", &bSize);
// Allocate space for the puzzle block and the word to be searched
char **block = (char **)malloc(bSize * sizeof(char *));
char *word = (char *)malloc(20 * sizeof(char));
// Read puzzle block into 2D arrays
for (i = 0; i < bSize; i++) {
*(block + i) = (char *)malloc(bSize * sizeof(char));
for (j = 0; j < bSize - 1; ++j) {
fscanf(fptr, "%c ", *(block + i) + j);
}
fscanf(fptr, "%c \n", *(block + i) + j);
}
fclose(fptr);
printf("Enter the word to search: ");
scanf("%s", word);
// Print out original puzzle grid
printf("\nPrinting puzzle before search:\n");
printPuzzle(block);
// Call searchPuzzle to the word in the puzzle
searchPuzzle(block, word);
return 0;
}
void printPuzzle(char **arr) {
// This function will print out the complete puzzle grid (arr).
// It must produce the output in the SAME format as the samples
// in the instructions.
// Your implementation here...
for (int row = 0; row < bSize; row++) {
for (int col = 0; col < bSize; col++) {
printf("%c ", *(*(arr + row) + col));
}
printf("\n");
}
printf("\n");
}
//pointer notation for the 8 possible moves
// possible moves at a certain location can be [-1 ,0], [-1, 1], [0, 1], [1, 1],
// [1, 0], [1, -1], [0, -1], [-1, -1]
bool ignoreCase(char a, char b) {
int offset = 'a' - 'A';
char lowA = (a >= 'A' && a <= 'Z') ? a + offset : a;
char lowB = (b >= 'A' && b <= 'Z') ? b + offset : b;
return lowA == lowB;
}
bool taken(int **path, int x, int y) {
return *(*(path + x) + y) > 0;
}
bool searchWord(char **arr, int x, int y, char *word, int index, int **path, int step, int n, int *startStep) {
int *dx = (int*) malloc(8 * sizeof(int));
int *dy = (int*) malloc(8 * sizeof(int));
*(dx + 0) = -1;
*(dy + 0) = 0;
*(dx + 1) = -1;
*(dy + 1) = 1;
*(dx + 2) = 0;
*(dy + 2) = 1;
*(dx + 3) = 1;
*(dy + 3) = 1;
*(dx + 4) = 1;
*(dy + 4) = 0;
*(dx + 5) = 1;
*(dy + 5) = -1;
*(dx + 6) = 0;
*(dy + 6) = -1;
*(dx + 7) = -1;
*(dy + 7) = -1;
if (index == strlen(word))
return true;
if (x < 0 || x >= n || y < 0 || y >= n ||
!ignoreCase(*(*(arr + x) + y), *(word + index))) { // check if out of bounds
return false;
}
int z = path[x][y];
if (z > 0) {
// Cell has been visited before, concatenate the current step
int digit = 1;
while(step - *startStep >= digit){
digit *= 10;
}
z = z * digit + (step - *startStep);
} else {
// First visit to the cell, set the step number
z = step - *startStep + 1;
}
*(*(path + x) + y) = z;
for (int dir = 0; dir < 8; dir++) {
if (searchWord(arr, x + *(dx + dir), y + *(dy + dir), word, index + 1, path, step + 1, n, startStep)) {
return true;
}
}
*(*(path + x) + y) = 0; // reset path
return false;
}
void searchPuzzle(char **arr, char *word) {
int **path = (int **)malloc(bSize * sizeof(int *));
for (int i = 0; i < bSize; i++) {
*(path + i) = (int *)malloc(bSize * sizeof(int));
memset(*(path + i), 0, bSize * sizeof(int)); // initialize with zeros
}
bool found = false;
bool secondPathFound = false;
int firstPathStartX = -1, firstPathStartY = -1;
int startStep = 1; // Start step for the first path
// Find the first path
for (int i = 0; i < bSize && !found; i++) {
for (int j = 0; j < bSize && !found; j++) {
if (ignoreCase(*(*(arr + i) + j), *(word + 0))) {
if (searchWord(arr, i, j, word, 0, path, startStep, bSize, &startStep)) {
found = true;
firstPathStartX = i;
firstPathStartY = j;
break;
}
*(*(path + i) + j) = 0;
}
}
}
if (found) {
printf("Word found!\n");
} else {
printf("Word not found!\n");
goto cleanup;
}
startStep = 1; // Reset start step for the second path
// Find the second path, excluding cells from the first path
for (int i = 0; i < bSize && !secondPathFound; i++) {
for (int j = 0; j < bSize && !secondPathFound; j++) {
if (ignoreCase(*(*(arr + i) + j), *(word + 0)) && !(i == firstPathStartX && j == firstPathStartY)) {
if (searchWord(arr, i, j, word, 0, path, startStep, bSize, &startStep)) {
secondPathFound = true;
break;
}
*(*(path + i) + j) = 0;
}
}
}
printf("Printing the search path:\n");
for (int i = 0; i < bSize; i++) {
for (int j = 0; j < bSize; j++) {
if (*(*(path + i) + j) > 0) {
printf("%-5d\t", *(*(path + i) + j)); // Print the path number with a fixed width
} else {
printf("%-5d\t", 0);
}
}
printf("\n");
}
printf("\n");
cleanup:
// Free the allocated memory for path
for (int i = 0; i < bSize; i++) {
free(*(path + i));
}
free(path);
}