2014-09-30 4 views
0

주 파일에서 단어를 칠 때까지 각 입력 줄을 반복 한 다음 puzzleArray를 사용하여 startSearch를 검색하는 단어를 전달하고 solvedArray를 반환합니다. 단어는 문자열, 행의 수는 크기, 열의 길이는 길이입니다.C 모든 방향으로 반복적 인 WordSearch 솔버

현재 세그먼트 분할 오류 또는 무한 루프가 계속 발생합니다. 내 알고리즘/코드 도움이 크게 주시면 감사하겠습니다.

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

"재귀 적"알고리즘보다 더 간단한 알고리즘을 제안합니다. 각 단어의 첫 글자를 검색 한 다음 발견 된 경우 8 가지 방향으로 비교합니다. (아마도 어쨌든 단어가 그 방향에 맞지 않는다면 조기 구제책이 될 것입니다.) – usr2564301

+0

훨씬 간단한 방법입니다. 재귀 적으로하는 이유는 해결 된 배열이 단어의 위치 만 보여줄 수 있기 때문입니다. – user2423436

답변

1
direction == "up" 

이 는 동일하게 두 문자열을 비교하는 방법이 아니다. 문자열 비교를 위해 strcmp/strncmp을 사용하십시오. 이러한 종류의 비교는 코드 전체에 나타납니다. 또한

:

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

j < puzzleArray[rowPos][colPos] != '\0'은 반신 반의하는 모습이, 당신은 무엇을 어떻게하려고?

+0

현재의 문자가 위치와 같으면 계속 진행합니다. 찾지 못하면 배열을 통해 위치를 찾습니다. – user2423436

+0

@ user2423436'j ouah

+0

이것을 지적 해 주셔서 고맙습니다. 대신 j user2423436