I've been working on this word search algorithm and had some issues about its speed. It kinda looks like it takes a little too much time.
If anyone could offer suggestions on making this better and improving my skills, that would be great.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bool Check(string Letter, string Matrix)
{
return (Letter == Matrix) ? true : false;
}
int main()
{
int Height = 0, Widht = 0, Numb = 0, Longer = 0, Look = 0, Look1 = 0, Look2 = 0, Look3 = 0, Poz = 0, Poz1 = 0;
int Loop = 0, Loop1 = 0, Loop2 = 0, Loop3 = 0, Loop4 = 0, Loop5 = 0, Loop6 = 0;
bool Found, Found1, Found2, Found3;
ifstream Data("Files/Info.txt");
Data >> Widht >> Height;
string Matrix[Height][Widht];
while(Loop < Height)
{
Loop1 = 0;
while(Loop1 < Widht)
{
Data >> Matrix[Loop][Loop1];
Loop1++;
}
Loop++;
}
Data >> Numb;
string Words[Numb];
while(Loop2 < Numb)
{
Data >> Words[Loop2];
Loop2++;
}
Data.close();
while(Loop3 < Numb)
{
Longer = Words[Loop3].size();
Loop4 = 0;
Loop6 = 0;
cout << Words[Loop3] << endl;
while(Loop4 < Height) // Looks horizantaly
{
Loop5 = 0;
Look = 0;
Look1 = 0;
while(Loop5 < Widht - Longer + 1)
{
Found = Check(Words[Loop3].substr(Look, 1), Matrix[Loop4][Loop5]);
if(Found)
{
Loop6 = 1;
Look = Loop5;
Poz = Loop4;
Poz1 = Loop5;
while(Loop6 < Longer)
{
Look++;
Found1 = Check(Words[Loop3].substr(Loop6, 1), Matrix[Loop4][Look]);
if(Found1)
{
Loop6++;
}
else
{
Loop5 += Loop6;
Loop6 = Longer + 1;
}
}
}
if(Loop6 == Longer)
{
cout << "Word " << Words[Loop3] << " was found at: " << Poz << " collumn " << Poz1 + 1 << " symbol. Horizontaly from right to left" << endl;
Loop5 = Widht;
Loop4 = Height;
Loop3++;
}
Loop5++;
}
Loop4++;
}
Loop3++;
}
return 0;
}
EDITED CODE
#include <iostream>
#include <string>
#include <fstream>
#include <new>
using namespace std;
//Loop[4] //Loop[5]
int * Possibles(int Widht, int Height, int Poz, int Poz1, int Leng, int * Possible)
{
if(Poz1 < Widht - Leng + 1) // To right
{
Possible[0] = 1;
}
if(Poz1 >= Leng - 1) // To left
{
Possible[1] = 1;
}
if(Poz <= Height - Leng) // From top to bottom
{
Possible[2] = 1;
}
if(Poz >= Leng) // From bottom to top
{
Possible[3] = 1;
}
return Possible;
}
int * Zero(int * Possible)
{
Possible[0] = 0;
Possible[1] = 0;
Possible[2] = 0;
Possible[3] = 0;
return Possible;
}
string Next(string ** Matrix, int Height, int Widht)
{
return Matrix[Height][Widht];
}
bool Find(string Word, int Poz, int Poz1, int Look, string Have, string ** Matrix)
{
if(Have == Word)
{
return true;
}
if(Word.substr(Look,1) == Next(Matrix, Poz, Poz1))
{
Have += Word.substr(Look,1);
return Find(Word, Poz, Poz1 + 1, Look + 1, Have, Matrix);
}
return false;
}
int main()
{
int Height = 0, Widht = 0, Numb = 0;
int Loop[] = {0, 0, 0, 0, 0, 0, 0};
int * Possible = new int[4];
string Search;
ifstream Data("Files/Info.txt");
Data >> Widht >> Height;
string ** Matrix = new string*[Height];
for(int i = 0; i < Widht; ++i)
{
Matrix[i] = new string[Widht];
}
while(Loop[0] < Height)
{
Loop[1] = 0;
while(Loop[1] < Widht)
{
Data >> Matrix[Loop[0]][Loop[1]];
Loop[1]++;
}
Loop[0]++;
}
Data >> Numb;
string Words[Numb];
while(Loop[2] < Numb)
{
Data >> Words[Loop[2]];
Loop[2]++;
}
Data.close();
while(Loop[3] < Numb)
{
Search = Words[Loop[3]].substr(0, 1);
Loop[4] = 0;
while(Loop[4] < Height)
{
Loop[5] = 0;
while(Loop[5] < Widht)
{
if(Matrix[Loop[4]][Loop[5]] == Search)
{
Zero(Possible);
Possibles(Widht, Height, Loop[4], Loop[5], Words[Loop[3]].size(), Possible);
if(Possible[0] == 1)
{
if(Find(Words[Loop[3]], Loop[4], Loop[5] + 1, 1, Search, Matrix))
{
cout << "Stupid word found !!!!!!!!! " << Words[Loop[3]] << endl;
}
}
cout << Possible[0] << " " << Possible[1] << " " << Possible[2] << " " << Possible[3] << " " << Next(Matrix, Loop[4], Loop[5]) << endl;
}
Loop[5]++;
}
Loop[4]++;
}
Loop[3]++;
}
for(int i1 = 0; i1 < Widht; ++i1)
{
delete [] Matrix[i1];
}
delete [] Matrix;
delete [] Possible;
return 0;
}
And infos file looks like this:
10 10
a s d f g h j k l o
e t a k t r e e a d
t e d a w e f g d a
r e s a g a f a q w
d c s q a z x v n b
q w e t a s f w p s
w a r y i w t o o a
s b b d s s w q o k
w e r a d g d a p a
q w r c b a x l s k
3
tree
poop
test
Of course, I will later add other directions in which words might be written.
I've improved the code by your suggestions. Can you suggest anything about its speed?