全方位C递归WordSearch解算器

全方位C递归WordSearch解算器

在主文件中,我循环遍历输入的每一行,直到命中单词为止,然后将其搜索单词传递给searchArray,它使用我想取回的solvedArray,单词作为字符串,大小作为行数,和长度为列数。

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

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;
}

最佳答案

direction == "up"


这不是比较两个字符串相等的方式。使用strcmp / strncmp进行字符串比较。这种比较会出现在整个代码中。

也:

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


j < puzzleArray[rowPos][colPos] != '\0'看起来可疑,您打算做什么?

关于c - 全方位C递归WordSearch解算器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26130885/

10-10 11:26