不确定为什么长度必须在此单词搜索代码中减1

时间:2016-11-12 05:28:04

标签: c multidimensional-array binary-search

我正在查看此代码以帮助我解决我正在研究的wordsearch问题。我在很大程度上理解,但我特别迷失在这一部分:

// No possible word can be formed, because we go off the grid.
if (!inbounds(startRow+(length-1)*DX[direction], startCol+(length-1)*DY[direction]))
    return NULL;

我不确定你为什么要减去长度为1?如果我理解第一次运行getWord() startRow时的代码应为0,则startCol应为0,长度为4,DX [direction]和DY [direction]都应为-1。这些值应该给inbounds() x = -3和y = -3,这将导致它超出范围。但是,当x = -4且y = -4时,为什么从长度中减去1会产生相同的结果呢?

#include <stdio.h>
#include <stdlib.h>

// Works better as a #define than const - dictionary file.
#define DICTIONARY_FILE "dictionary.txt"

// Maximum dictionary word size.
const int MIN_SIZE = 4;
const int MAX_SIZE = 19;

// Constants for directional movement.
const int NUM_DIRECTIONS = 8;
const int DX[] = {-1,-1,-1,0,0,1,1,1};
const int DY[] = {-1,0,1,-1,1,-1,0,1};

// Function prototypes
char** loadDictionary(char fileName[]);
char* getWord(char** grid, int startRow, int startCol, int direction, int length);
void freeGrid(char** array, int numRows);
int inbounds(int x, int y);
int binSearch(char** dictionary, char* tryWord);

// Global variables, for ease of implementation.
int dictionary_size, row, col;

int main() {

    // Load the dictionary at the very beginning!
    char** dictionary = loadDictionary(DICTIONARY_FILE);

    // Get number of cases.
    int loop, numCases;
    scanf("%d", &numCases);

    // Process each case.
    for (loop=1; loop<=numCases; loop++) {

        int i, j, k, len;

        // Case header
        printf("Words Found Grid #%d:\n", loop);

        // Get grid dimensions, allocate space(don't forget '\0'), and read in words.
        scanf("%d%d", &row, &col);
        char** grid = malloc(sizeof(char*)*row);
        for (i=0; i<row; i++) {
            grid[i] = malloc(sizeof(char)*(col+1));
            scanf("%s", grid[i]);
        }

        // Generate each possible sequence of letters in the grid.
        for (i=0; i<row; i++) {
            for (j=0; j<col; j++) {
                for (k=0; k<NUM_DIRECTIONS; k++) {
                    for(len=MIN_SIZE; len<=MAX_SIZE; len++) {

                        // Generate this word.
                        char* tryWord = getWord(grid, i, j, k, len);

                        // Only want to search if the string exists.
                        if (tryWord != NULL) {

                            // If we find it, print it.
                            if (binSearch(dictionary, tryWord))
                                printf("%s\n", tryWord);

                            // No longer need this!
                            free(tryWord);
                        }
                    }
                }
            }
        }

        // We can get rid of this space now.
        freeGrid(grid, row);
    }

    // Clean up.
    freeGrid(dictionary, dictionary_size);
    return 0;
}

// Returns the sequence of length letters in grid starting at (startRow, startCol), heading
// in the numerical direction (0-7) dictated by the constants DX and DY. Returns NULL if
// no such word exists because it would force us to go out of bounds.
char* getWord(char** grid, int startRow, int startCol, int direction, int length) {

    // No possible word can be formed, because we go off the grid.
    if (!inbounds(startRow+(length-1)*DX[direction], startCol+(length-1)*DY[direction]))
        return NULL;

    // Store answer here.
    char* res = malloc(sizeof(char)*(length+1));

    // Copy in letters and NULL character.
    int i;
    for (i=0; i<length; i++)
        res[i] = grid[startRow+i*DX[direction]][startCol+i*DY[direction]];
    res[length] = '\0';
    return res;
}

// Returns 1 iff (x,y) is inbounds of the grid.
int inbounds(int x, int y) {
    return x >= 0 && y >= 0 && x < row && y < col;
}

0 个答案:

没有答案