首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C所有方向的递归WordSearch求解器

C所有方向的递归WordSearch求解器
EN

Stack Overflow用户
提问于 2014-10-01 06:02:32
回答 1查看 1.3K关注 0票数 0

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

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-10-01 06:05:05

代码语言:javascript
复制
direction == "up"

这不是比较两个字符串是否相等的方法。使用strcmp / strncmp进行字符串比较。这种比较在你的代码中随处可见。

另外:

代码语言:javascript
复制
for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26130885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档