Question

Write a program to solve the word puzzle problem. Explain your code.

Write a program to solve the word puzzle problem. Explain your code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int namesearch( char p[], char t[] )

{

   int i, j, k, m = strlen( p ), n = strlen( t ),

       pFP, tFP;

   pFP = fp( p, 0, m );   tFP = fp( t, 0, m );

   for ( i = 0; i <= n-m; ++i ) {

      if ( pFP == tFP ) {

         for ( j = 0, k = i; j < m; ++j, ++k )

            if ( p[ j ] != t[ k ] )

               break;

         if ( j == m ) return i;

      }

      else if ( i + m < n ) tFP += t[ i+m ] - t[ i ]; else break;

   }

   return -1;

}

int fp( char s[], int i, int m )

{

   int j, sum;

   sum = 0;

   for ( j = i; j < m; ++j )

      sum += s[ j ];

   return sum;

}

Assume that the puzzle is given in the text file wpuzzle.txt. The first line contains two numbers (n and m) separated by a blank space. Those numbers are the rows and columns of the puzzle and are followed by n lines of m characters each. Assume that the words to be found are given in the text file words.txt, one word per line. No assumptions should be made as to whether or not the words appear in the puzzle. The program must fill (and print) a solution array with the words in the position and direction in which they were found

#define maxN 50

#define maxM 50

char puzzle[ maxN ][ maxM ], // puzzle array

     puzsol[ maxN ][ maxM ], // solution array

       line[ maxM + 1 ];     // input line from file

int n,                      // actual number of rows

     m,                      // actual number of columns

     maxOFnm,                // number of elements in main diagonals

     pFP;                    // fingerprint of a word (pattern)

void main()

{

   FILE *fp;

  int len;

   if ( LoadPuzzle() ) {

      ShowPuzzle();

      if ( (fp = fopen( "words.txt", "r" )) != NULL ) {

         while ( fgets( line, maxM + 1, fp ) != NULL ) {

            len = removeNL( line );

            FindWord( line, len );

         }

         fclose( fp );

         ShowSolution();

      }

   }

}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
Write a program to solve the word puzzle problem. Explain your code.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT