-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordSearchMultithread.c
205 lines (166 loc) · 4.76 KB
/
wordSearchMultithread.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
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#define boolean int
#define false 0
#define true 1
int threadRow = 0;
int threadCol = 0;
boolean result = false;
pthread_mutex_t lockCol;
pthread_mutex_t lockRow;
typedef struct {
char** puzzle;
int size;
} Puzzle;
typedef struct {
Puzzle* puzzle;
char* word;
} findWordArgs;
boolean solve(Puzzle* puzzle, int row, int col, char* wordToFind, int letterNo);
int getRow(){
pthread_mutex_lock(&lockRow);
int ret = threadRow;
threadRow++;
pthread_mutex_unlock(&lockRow);
return ret;
}
int getCol(){
pthread_mutex_lock(&lockCol);
int ret = threadCol;
threadCol++;
pthread_mutex_unlock(&lockCol);
return ret;
}
int nextRow(int row, int direction, int size){
// Directions are as follows
/*
* 7 0 1
* 6 - 2
* 5 4 3
*/
if (direction == 0 || direction == 1 || direction == 7){
return (row - 1 + size) % size;
}
else if (direction >= 3 && direction <= 5){
return (row + 1) % size;
}
else {
return row;
}
}
int nextCol(int col, int direction, int size){
if (direction >= 1 && direction <= 3){
return (col + 1) % size;
}
else if (direction >= 5 && direction <= 7){
return (col - 1 + size) % size;
}
else {
return col;
}
}
boolean valid(int row, int col, Puzzle* puzzle){
boolean withinLimits = (row >= 0) && (col >= 0) && (row < puzzle->size) && (col < puzzle->size);
boolean isLower = puzzle->puzzle[row][col] >= 'a' && puzzle->puzzle[row][col] <= 'z';
if (withinLimits && isLower)
return true;
return false;
}
void* findWord(void* args){
Puzzle* puzzle = ((findWordArgs*)args)->puzzle;
char* word = ((findWordArgs*)args)->word;
for (int i = getRow(); i < puzzle->size; i = getRow()){
for (int j = getCol(); j < puzzle->size; j = getCol()){
if (puzzle->puzzle[i][j] == word[0]){
puzzle->puzzle[i][j] -= 32;
if (solve(puzzle, i, j, word, 1)){
result = true;
}
else {
puzzle->puzzle[i][j] += 32;
}
}
}
if (getCol() >= puzzle->size){
pthread_mutex_lock(&lockCol);
threadCol = 0;
pthread_mutex_unlock(&lockCol);
}
}
return NULL;
}
boolean solve(Puzzle* puzzle, int row, int col, char* wordToFind, int letterNo){
if (letterNo == strnlen(wordToFind, puzzle->size * puzzle->size)){
return true;
}
for (int i = 0; i < 8; i++){
int nRow = nextRow(row, i, puzzle->size);
int nCol = nextCol(col, i, puzzle->size);
if (valid(nRow, nCol, puzzle) && puzzle->puzzle[nRow][nCol] == wordToFind[letterNo]){
puzzle->puzzle[nRow][nCol] -= 32;
if (solve(puzzle, nRow, nCol, wordToFind, letterNo+1)){
return true;
}
puzzle->puzzle[nRow][nCol] += 32;
}
}
return false;
}
Puzzle* readPuzzle(char* file){
FILE* readIn = fopen(file, "r");
char tmp;
int size = 0;
fscanf(readIn, "%d", &size);
fseek(readIn, 2, SEEK_CUR);
char** pzl = calloc(sizeof(char*), size);
for (int i = 0; i < size; i++){
pzl[i] = calloc(sizeof(char), size);
}
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
fread(&pzl[i][j], sizeof(char), 1, readIn);
}
fseek(readIn, 2, SEEK_CUR);
}
Puzzle* puzzle = calloc(sizeof(Puzzle), 1);
puzzle->puzzle = pzl;
puzzle->size = size;
fclose(readIn);
return puzzle;
}
void printPuzzle(Puzzle* puzzle){
for (int i = 0; i < puzzle->size; i++){
for (int j = 0; j < puzzle->size; j++){
printf("%c", puzzle->puzzle[i][j]);
}
printf("\n");
}
}
int main(int argc, char **argv){
Puzzle* pzl = readPuzzle(argv[1]);
char* f = argv[2];
int numThreads = atoi(argv[3]);
findWordArgs* args = calloc(sizeof(findWordArgs), 1);
args->puzzle = pzl;
args->word = f;
pthread_mutex_init(&lockRow, NULL);
pthread_mutex_init(&lockCol, NULL);
pthread_t threads[numThreads];
for (int i = 0; i < numThreads; i++) {
pthread_create(threads+i, NULL, findWord, args);
}
for (int i = 0; i < numThreads; i++) {
pthread_join(threads[i], NULL);
}
if (result){
printf("%s was found!\n", f);
printPuzzle(pzl);
}
else {
printf("%s was not found\n", f);
}
free(pzl);
return 0;
}