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;
}
The 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.