C向各个方向递归WordSearch解算器

时间:2014-09-30 22:02:32

标签: c recursion wordsearch

在主文件中,我遍历每一行输入,直到它击中单词,然后我将其搜索的单词传递给使用puzzleArray的startsSearch,我想要返回的solveArray,单词作为字符串,大小作为数字行数和长度作为列数。

目前,我一直在收到分段错误/无限循环。对我的算法/代码的任何帮助将不胜感激。

void startSearch(char** puzzleArray,char** solvedArray,char* string,int size,int length)
{
    char* direction = "";
    int solved = 1;
    int j = 0;

    while( j <= 7 && solved != 0)
    {
        if(j == 0)
        {
            direction = "up";
        }
        else if(j == 1)
        {
            direction = "upRight";
        }
        else if(j == 2)
        {
            direction = "right";
        }
        else if(j == 3)
        {
            direction = "downRight";
        }
        else if(j == 4)
        {
            direction = "down";
        }
        else if(j == 5)
        {
            direction = "downLeft";
        }
        else if(j == 6)
        {
            direction = "left";
        }
        else if(j == 7)
        {
            direction = "upLeft";
        }
        solved = recursiveSearch(puzzleArray,solvedArray,string,direction,size,length,0,0,0);
        j++;
    }

}

int recursiveSearch(char** puzzleArray,char** solvedArray,char* string,char* direction,int sizeOfPuzzle,int lengthOfArrayWithSpaces,int rowPos,int colPos,int stringPosition)
{
    int lengthOfWord;
    int i = rowPos;
    int j = colPos;
    int found = 0;
    int empty = 1;
    char c = string[stringPosition];
    int position = stringPosition;
    lengthOfWord = lengthOfArray(string);

    if(string[position+1] == '\0')
    {
        return 0;
    }
    while(empty != 0)
    {
        if(string[stringPosition] == puzzleArray[i][j])
        {
            found = 1;
        }
        else if(rowPos < sizeOfPuzzle && colPos < lengthOfArrayWithSpaces)
        {
            stringPosition = 0;
            for(i = rowPos; i < sizeOfPuzzle && found != 1; i++)
            {
                for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)
                {
                    if(string[stringPosition] == puzzleArray[i][j])
                    {
                        found = 1;
                        rowPos = i;
                        colPos = j;
                        stringPosition = 0;
                    }
                }
            }
            if(found == 0)
            {
                empty = 1;
            }
        }

        if(found == 1)
        {
            position = stringPosition + 1;
            if(rowPos-1 >= 0)
            {
                //printf("\nString:%cPuzzleArray:%c",string[position],puzzleArray[rowPos-1][colPos]);
                if(string[position] == puzzleArray[rowPos-1][colPos] && direction == "up")
                {
                    //printf("UP");
                    if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos,position+1))
                    {
                        solvedArray[rowPos-1][colPos] = puzzleArray[rowPos-1][colPos];
                        return 0;
                    }
                }
                else if(colPos+2 <= lengthOfArrayWithSpaces)
                {
                    if(string[position] == puzzleArray[rowPos-1][colPos+2] && direction == "upRight")
                    {
                        if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos+2,position+1))
                        {
                            solvedArray[rowPos-1][colPos+2] = puzzleArray[rowPos-1][colPos+2];
                            return 0;
                        }
                    }
                }
            }
            if(colPos+2 <= lengthOfArrayWithSpaces)
            {
                if(string[position] == puzzleArray[rowPos][colPos+2] && direction == "right")
                {
                    if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos,colPos+2,position+1))
                    {
                        solvedArray[rowPos][colPos+2] = puzzleArray[rowPos][colPos+2];
                        return 0;
                    }
                }
                if(rowPos+1 <= lengthOfArrayWithSpaces)
                {
                    if(string[position] == puzzleArray[rowPos+1][colPos+2] && direction == "downRight")
                    {
                        if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
                        {
                            solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
                            return 0;
                        }
                    }
                }
            }
            if(rowPos+1 <= sizeOfPuzzle)
            {
                if(string[position] == puzzleArray[rowPos+1][colPos] && direction == "down")
                {
                    if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos,position+1))
                        {
                            solvedArray[rowPos+1][colPos] = puzzleArray[rowPos+1][colPos];
                            return 0;
                        }
                }
                if(rowPos + 1 <= lengthOfArrayWithSpaces)
                {
                    if(string[position] == puzzleArray[rowPos+1][colPos-2] && direction == "downLeft")
                    {
                        if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos-2,position+1))
                        {
                            solvedArray[rowPos+1][colPos-2] = puzzleArray[rowPos+1][colPos-2];
                            return 0;
                        }
                    }
                }
            }
            if(colPos-2 >= 0)
            {
                if(string[position] == puzzleArray[rowPos][colPos-2] && direction == "left")
                {
                    if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
                        {
                            solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
                            return 0;
                        }
                }
                if(rowPos - 1 >= 0)
                {
                    if(string[position] == puzzleArray[rowPos-1][colPos-2] && direction == "upLeft")
                    {
                        if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos-2,position+1))
                        {
                            solvedArray[rowPos-1][colPos-2] = puzzleArray[rowPos-1][colPos-2];
                            return 0;
                        }
                    }
                }
            }
        }
    }

    return 1;
}

1 个答案:

答案 0 :(得分:1)

direction == "up"

这不是你如何比较两个字符串是否相等。使用strcmp / strncmp进行字符串比较。这种比较会出现在您的代码中。

此外:

for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)

j < puzzleArray[rowPos][colPos] != '\0'看起来很可疑,你想做什么?