gpt4 book ai didi

C 全方位递归 WordSearch 求解器

转载 作者:行者123 更新时间:2023-11-30 15:29:39 25 4
gpt4 key购买 nike

在主文件中,我循环遍历输入的每一行,直到它遇到单词,然后我将其搜索的单词传递给 startSearch with puzzleArray,我想要返回的已解决数组,单词为字符串,大小为数字行数,长度为列数。

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

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 全方位递归 WordSearch 求解器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26130885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com