Well I have created this one and on my computer it is working. I would like to hear about some alternative solutions.
INTRODUCTION
This time I have one algorithm, but to understand it we need to consider a few terms. First of all, we should be familiar with the pangrams.
For example: “The quick brown fox jumps over lazy dog” is the famous one. The pangram has all letters of a particular alphabet.
Yes, there are perfect pangrams they have all the letters, but they don’t repeat more than once.
In this case we will use: “D. V. Pike flung J. Q. Schwartz my box“.
THE PROBLEM
In the matrix we have placed our perfect pangram each letter is surrounded with the next one. By surrounded I mean that each letter is on distance no further than one position: left, right, up, down or diagonally.
So, our aim is to find the path that starts at the beginning position and forms unbroken line toward the end.
The found path is the programs output.
Matrix:
* V I E *
D P K F L
X O B N U
Z M Y G J
T R W S Q
* A H C *
SOLUTION:
ANALYSIS:
In the solution, we start from the first letter, and then we search for the second one in surrounding of the first one, after we have found second letter we look for the third one and so on till we find all letters from the perfect pangram.
When I say surrounding, I mean that if the current location is (m, n), the next letter could be found at:
(m-1,n), (m+1,n), (m,n+1), (m,n-1), (m-1,n-1), (m+1, n-1), (m+1, n+1) or (m-1, n+1).
Because it is a perfect pangram, we can form continual line that fills the given matrix.
CODE:
We have two files, one is main.cpp and the other is Functions.h file. If you don’t like this approach you could create one file and run the program.
main.cpp
#include <iostream>
#include "Functions.h"
using namespace std;
int
main( void)
cout<<MESSAGE_1<<endl;
WritteMatrix(DIM_ROW,DIM_COL,cMatrix);
cout<<MESSAGE_2<<endl;
WritteArray(DIM_ROW*DIM_COL-4,cPerfectPangram);
cout<<endl;
//FIND THE LETTER D
int xStart=0, yStart=0;
FindStart( xStart, yStart,
DIM_ROW,DIM_COL,
cMatrix);
//FIND THE PATH
int Koordinat[DIM_ROW*DIM_COL-4][2];
Koordinat[0][0]=xStart; Koordinat[0][1]=yStart;
FindThePath(DIM_ROW*DIM_COL-4,
Koordinat,
DIM_ROW*DIM_COL-4,
cPerfectPangram,
DIM_ROW, DIM_COL,
cMatrix,
xStart, yStart);
int p; cin>>p;
return EXIT_SUCCESS;
}
Functions.h
#include <iostream>
#define DIM_ROW 6
#define DIM_COL 5
#define WORDS_TOTAL 26
#define MESSAGE_1 "In the matrix"
#define MESSAGE_2 "Looking for the sentence"
char cMatrix[DIM_ROW][DIM_COL]=
{{'*', 'V', 'I', 'E', '*'},
{'D', 'P', 'K', 'F', 'L'},
{'X', 'O', 'B', 'N', 'U'},
{'Z', 'M', 'Y', 'G', 'J'},
{'T', 'R', 'W', 'S', 'Q'},
{'*', 'A', 'H', 'C', '*'}};
char cPerfectPangram[]={'D','V','P','I','K',
'E','F','L','U','N',
'G','J','Q','S','C',
'H','W','A','R','T',
'Z','M','Y','B',
'O','X'};
void
WritteMatrix( int i, int j,
char cMatrix[DIM_ROW][DIM_COL])
{
for(int i=0; i<DIM_ROW; i++)
{
for(int j=0; j<DIM_COL; j++)
std::cout<<cMatrix[i][j];
std::cout<<std::endl;
}
}
void
WritteArray( int n, char cNiz[])
{
for(int i=0; i< n; std::cout<<cNiz[i++]<<" ");
}
void
FindStart(int& iX, int& iY,
int i, int j,
char cMatrix[DIM_ROW][DIM_COL])
{
for(i=0; i<DIM_ROW; i++)
for(j=0; j<DIM_COL; j++)
if( cMatrix[i][j]=='D')
{
iX=i; iY=j; break;
}
}
bool
OK( int i, int j)
{
bool bResult=true;
if((i<0)||(i>=6)) bResult =false;
if((j<0)||(j>=5)) bResult =false;
return bResult;
}
void
FindThePath(int iDimenzija ,
int Koordinat[DIM_ROW*DIM_COL-4][2],
int iPangram ,
char cPerfektPangram[DIM_ROW*DIM_COL],
int iRed,int iKol,
char cMatrix[DIM_ROW][DIM_COL],
int xStart,int yStart)
{
for(int i=1; i< DIM_ROW*DIM_COL-4; i++)
{
char cTrazeni=cPerfektPangram[i];
int x=xStart, y=yStart;
if( OK(++x,++y)&&(cTrazeni == cMatrix[x][y]))
{
Koordinat[i][0]=x; ++xStart;
Koordinat[i][1]=y; ++yStart;
continue;
}
x=xStart, y=yStart;
if(OK(--x,--y)&&( cTrazeni == cMatrix[x][y]))
{
Koordinat[i][0]=x; --xStart;
Koordinat[i][1]=y; --yStart;
continue;
}
x=xStart, y=yStart;
if(OK(++x,--y)&&(cTrazeni == cMatrix[x][y]))
{
Koordinat[i][0]=x; ++xStart;
Koordinat[i][1]=y; --yStart;
continue;
}
x=xStart, y=yStart;
if(OK(--x,++y)&&( cTrazeni == cMatrix[x][y]))
{
Koordinat[i][0]=x; --xStart;
Koordinat[i][1]=y; ++yStart;
continue;
}
x=xStart, y=yStart;
if( OK(--x,y)&&(cTrazeni == cMatrix[x][y]))
{
Koordinat[i][0]=x; --xStart;
Koordinat[i][1]=y;
continue;
}
x=xStart, y=yStart;
if(OK(++x,y)&&(cTrazeni == cMatrix[x][y]))
{
Koordinat[i][0]=x; ++xStart;
Koordinat[i][1]=y;
continue;
}
x=xStart, y=yStart;
if(OK(x,++y)&&(cTrazeni == cMatrix[x][y]))
{
Koordinat[i][0]=x;
Koordinat[i][1]=y; ++yStart;
continue;
}
x=xStart, y=yStart;
if( OK(x,--y)&&(cTrazeni == cMatrix[x][y]) )
{
Koordinat[i][0]=x;
Koordinat[i][1]=y; --yStart;
continue;
}
}
std::cout<<"PATH IS"<<std::endl;
for(int i=0; i< DIM_ROW*DIM_COL-4; i++)
std::cout<<Koordinat[i][0]<<" "<<Koordinat[i][1]<<std::endl;
}