Поиск слова с использованием многомерных массивов

Предполагается, что эта программа позволяет пользователю вводить слово для поиска в многомерном массиве, заполненном символами. Слово может быть в любом недиагональном направлении.

Как написано, он найдет только правильное местоположение для длинного слова из одного символа. Я понимаю, что он найдет только первый экземпляр любого введенного слова, но это нормально. Я не могу понять, почему он не найдет слова длиннее одного символа.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <string.h>

#define GRIDSIZE 10
#define MAXSIZE 15   //Max size for word to search
#define SENTINEL -1

void print_grid(int[][GRIDSIZE], int);
int search_grid(int[][GRIDSIZE], int, int[], int, int[], int[]);

int main(void){
    int grid[GRIDSIZE][GRIDSIZE];
    int grid_size;
    char word[MAXSIZE];
    int word_int[MAXSIZE];
    int a,b,i,j,h;
    int word_size;
    int word_found;
    int go_again;
    int location_row[word_size];
    int location_column[word_size];

    srand(time(NULL));

    //fills grid with random letters
    for(i=0; i<10; i++){
        for(j=0; j<10; j++){
            do{grid[i][j] = rand() %  123;
            }while(grid[i][j]<97);
        }}


    //gets word and searches for it, returns 1 or 0 for found or not
    //exits of sentinel is entered
    do{
    print_grid(grid, grid_size);
    printf("Please enter a word to search followed by 'ENTER':\n");
    scanf(" %s", word);

    word_size = strlen(word);

    for(b=0; b < word_size; b++){
        word_int[b] = word [b];
    }

    word_found = search_grid(grid, grid_size, word_int, word_size, location_row, location_column);
    ("%d", word_found);

    if(word_found == 0){
        printf("\nThe word you entered was not found\n");
        printf("Enter '1' to search for another word, or '0' to exit, followed by 'ENTER'\n");
        scanf(" %d", &go_again);
    }

    }while(word_found != 1 && go_again == 1);



    if(word_found == 1){
        printf("The word you entered was found at:\n");
        for(h=0; h < word_size; h++){
            printf("%c%d: ", toupper(location_row[h] + 97), location_column[h]+1);
        }
        print_grid(grid, grid_size); 
    }

    return 0;
}

//prints N*N grid with labels on each axis
void print_grid(int grid[][GRIDSIZE], int grid_size){
    int i,j,k;
    char row[26] ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    printf("\n");

printf("   ");
for(k=1; k < GRIDSIZE +1; k++){
            printf("%d ", k);}

printf("\n");

for(i=0; i<10; i++){
    printf("%-c  ", row[i]);
        for(j=0; j<10; j++){
            printf("%-c ", grid[i][j]);
        }
    printf("\n");
}
printf("\n");
    return;
}

//searches grid for word
int search_grid(int grid[][GRIDSIZE], int gride_size, int word_int[], int word_size, int location_row[], int location_column[]){
    //printf("SEARCH GRID RAN//////////////////////");

    int r, c, q;
    int num = 0;
    int found[word_size];
    int found_total = 0;


    for(q=0; q < word_size; q++){
        found[q] = 0;}

    //search loops
    for(r=0; r < GRIDSIZE; r++){
        for(c=0; c < GRIDSIZE; c++){
            if(grid[r][c] == word_int[num]){
                found[num] = 1;
                location_row[num] = r;
                location_column[num] = c;
                    for(num=1; num < word_size; num ++){
                        if(r=0){
                        if(c=0){
                            if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK DOWN AND RIGHT
                        }
                        if(c = GRIDSIZE - 1){
                            if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK DOWN AND LEFT
                        }
                        else{
                            if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK LEFT RIGHT DOWN
                        }
                    }
                        if(r = GRIDSIZE - 1){
                        if(c=0){
                            if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK up AND RIGHT
                        }
                        if(c = GRIDSIZE - 1){
                            if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK UP AND LEFT
                        }
                        else{
                            if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK LEFT RIGHT UP
                        }
                    }
                        else{
                        if(c=0){
                            if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK UP DOWN AND RIGHT
                        }
                        if(c = GRIDSIZE - 1){
                            if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK UP DOWN AND LEFT
                        }
                        else{
                            if(grid[location_row[num-1]][location_column[num-1]-1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]][location_column[num-1]+1] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]-1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                            if(grid[location_row[num-1]+1][location_column[num-1]] == word_int[num])
                                found[num] = 1;
                                location_row[num] = r;
                                location_column[num] = c;
                    //CHECK LEFT RIGHT UP DOWN
                        }
                    }
                }
            }
        }
    }


    for(q=0; q < word_size; q++){
        found_total += found[q];
    }
    //printf("FOUND TOTAL %d", found_total);

    if(found_total == word_size){
           return 1;
        }

    if(found_total != word_size){
        return 0;
}
}

0 ответов

Другие вопросы по тегам