Advertisement
framp

Finding crossed words (commented)

May 23rd, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool checkV(char * C, int m, int n, char * P, int lung, int posM, int posN){
  5.   cout << "Looking for vertical match in " << posN << "-" << posN+lung-1 << "..\n";
  6.   for (int j=posN; j<posN+lung; j++){
  7.     int startM = posM-(j-posN); cout << "Starting from " << startM << "x" << j << "\n";
  8.     int c = 0;
  9.     if (startM>=0 && startM+lung<=m)
  10.       for (int i=startM; i<startM+lung; i++){
  11.     cout << i << "x" << j << ": " << C[i*n+j] << " ";
  12.     if (C[i*n+j]==P[c]){    cout << ((c==lung-1) ? "MATCH\n" : "found\n");
  13.       if (c==lung-1) return true;
  14.       c++;
  15.     }else           cout << "nothing to do\n";
  16.       }
  17.     else            cout << "There is not enough space in this column\n";
  18.   }
  19.   return false;
  20. }
  21.  
  22. bool check(char * C, int m, int n, char * P, int lung){
  23.   cout << "Looking for horizontal match..\n";
  24.   for (int i=0; i<m; i++){
  25.     int c = 0;
  26.     for (int j=0; j<n; j++){    cout << i << "x" << j << ": " << C[i*n+j] << " ";
  27.       if (C[i*n+j]==P[c]){  cout << ((c==lung) ? "MATCH\n" : "found\n");
  28.     if (c==lung-1 && checkV(C, m, n, P, lung, i, j-c))
  29.       return true;
  30.     c++;
  31.       }else if (n-j<=lung){     cout << "not enough space left in this row\n";
  32.     break;
  33.       }else         cout << "nothing to do\n";
  34.     }
  35.   }
  36.   return false;
  37. }
  38.  
  39.  
  40.  
  41. int main(){
  42.   int m = 6, n=8;
  43.   char C[48] = {
  44.     'a', 's', 'd', 'p', 'g', 'h', 'j', 'p',
  45.     'e', 'r', 'p', 'i', 'p', 'p', 'o', 'i',
  46.     'r', 'r', 'i', 'l', 'h', '5', 'r', 'p',
  47.     'y', 'y', 'p', 'p', 'i', 'p', 'p', 'p',
  48.     't', 'y', 'o', 'p', 'i', 'p', 'p', 'o',
  49.     'f', 'g', 'r', 'w', 'a', 'r', 'g', 'h'
  50.   };
  51.   int lung = 5;
  52.   char P[5] = {'p', 'i', 'p', 'p', 'o'};
  53.  
  54.   cout << check(&C[0], m, n, &P[0], lung) << "\n";
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement