/* file : wordMaze.c */ /* author : Oscar MirĂ³ (o.miro.i.lopez.feliu@student.rug.nl) */ /* Description: * Given the size of the matrix, the matrix and a word, finds if its possible to draw * the word by first finding the first letter, then the next letter only if its adjacent * to the first and so on. */ #include /////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////// // // ////// ERROR IS BECAUSE IF FIRST LETTER BRANCH DOESNT WORK IT DIES ////// SET FIRST LETTER TO '/' THEN RECALL // // /////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////// // this reads the matrix given void readGrid(int row, int col, char grid[row][col]) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { scanf(" %c", &grid[i][j]); } } } // finds the length of a char arr, used to find length of word int getLength (char word[]) { int count = 0; for (int i = 0; i < 102; i++) { if (word[i] == 0) { return count; } count++; } } // searches for a specific letter, only searching adjacent index to input // only happens for non first letters int searchRow (int row, int col, char grid[row][col], int *r, int *c, int let) { // bc the way its coded. in case the first letter was in (0, 0), we cant // have the loop start at i = -1 if (*r == 0) *r == 1; if (*c == 0) *c == 1; // 1 row before r, 1 row after for (int i = *r-1; i < *r+2; i++) { // 1 column before c, 1 column after for (int j = *c-1; j < *c+2; j++) { // if it equals the letter we are looking for, updates r and c, sets the // index to a non letter to avoid selecting it again and returns true if (grid[i][j] == let) { *r = i; *c = j; grid[i][j] = '/'; return 1; } } } return 0; } // made to search the first letter int searchFirst (int row, int col, char grid[row][col], int *r, int *c, int let) { // iterates through the entire matrix for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { // if its the first letter, save r and c, then equal the index to a non letter if (grid[i][j] == let) { *r = i; *c = j; grid[i][j] = '/'; return 1; } } } return 0; } // calls all the funcions earlier int findWord (int row, int col, char grid[row][col], char word[], int *x, int *y) { int size = getLength(word); int r = 0, c = 0; // if finds first letter if (searchFirst(row, col, grid, &r, &c, word[0])) { *x = c; *y = r; // finds all the remaining letters for (int i = 1; i < size; i++) { // if one cant be found, returns 0 if(!searchRow(row, col, grid, &r, &c, word[i])) { findWord (row, col, grid, word, x, y); return 0; } } } else { return 0; } return 1; } // main func, only declares all vars then checks result and prints int main (int argc, char *argv[]) { int row, col; scanf("%d %d", &row, &col); char grid[row][col]; readGrid(row, col, grid); char word[101]; scanf("%s", word); int x, y; int value = findWord(row, col, grid, word, &x, &y); if (value) { printf("%d %d\n", y, x); } else { printf("NOT FOUND\n"); } }