1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct position{
int x;
int y;
};//new type
char kboard[50][50];
int r ,c;
int distance (position a,position b)//calculates position between two keys given 2 positions
{
return (abs(a.x-b.x)+abs(a.y-b.y));
}
///////////////////////////////////////////////////////////////////////////////////
bool search_letter (const char& c,int l,int k)//searches for a letter before the position given in argument l for x and k for y
{
if((l==0)&&(k==0)) return(0);
for (int i=0;i<=(l-1);i++)
{
for(int j=0;j<=c;j++)
{
if (kboard[i][j]==c)return(1);
}
}
for(int j=0;j<k;j++)
{
if (kboard[(l-1)][j]==c)return(1);
}
return(0);
}
////////////////////////////////////////////////////////////////////////////////////////////
bool ligible(const char& c,int k, int l)//returns 1 if the letter is ligible to be there
{
if(l==0)
if(kboard[k-1][0]==c) return (1);
else{return (0);
}
if(k==0)
if(kboard[0][l-1]==c) return (1);
else{return (0);
}
else{
if((kboard[k-1][l]==c)||(kboard[k][l-1]==c))return(1);
else{return(0);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
position next_position(position a){
position w;
if (a.y!=(c-1)){
w.y+=1;
}
else{
if (a.x!=(r-1)){
w.x+=1;
}
else{w.x=-1;
}
}
return(w);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
position search_position(const char& c,position q)//search for a letter from a certain position to the end and returns the first adresse found ,if the letter is not found it returns a postion with x=-1
{
q=next_position(q);
if(q.x=-1)return(q);
for (int i=(q.x);i<r;i++)
{
for(int j=(q.y);j<c;j++){
if ((kboard[i][j])==c){
q.x=i;
q.y=j;
return(q);
}
}
}
q.x=-1;
return(q);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////main
int main(){
do {
cout<<"give the numbre of rows 0<r<51"<<endl;
cin>>r;
}while((r<1)||(r>50));
/////
do {
cout<<"give the numbre of colomns 0<c<51"<<endl;
cin>>c;
}while((c<1)||(c>50));
////////
char s ;
noasterisc :for (int i=0;i<r;i++)
{
for (int j=0;j<c;j++){
here :cout <<"enter the letter in the place : ("<<i<<","<<j<<")"<<endl;
cin>>s;
if (!(search_letter(s,i,j))) kboard[i][j]=s;
else{
if (ligible(s,i,j))kboard [i][j]=s;
else{
goto here;
}
}
}
}
if(!(search_letter('*',r,c))) {
cout<<"you didn't write the necessary asterisc *,rewrite the keyboard "<<endl;
goto noasterisc;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
char word[10001];
cout<<"enter a word "<<endl<<endl;
cin>>word;
int number_strokes=0,bestnbr=101,crnbr;
position pos1,pos2;
pos1.x=0;
pos1.y=0;
pos2=pos1;
pos2=search_position(word[0],pos2);
do {
crnbr=distance(pos1,pos2);
if (crnbr<bestnbr)bestnbr=crnbr;
pos2=search_position(word[0],pos2);
}while((pos2.x)!=-1);
numbre_strokes+=bestnbr;
bestnbr=101;
char c1,c2;
c1=word[0];
pos2=pos1;
for (int i=1;i<100001;i++){
if(word[i]=='\0')break;
else{
c2=word[i];
pos1=search_position(c1,pos1);
pos2=search_position(c2,pos2);
do {
do{
crnbr=distance(pos1,pos2);
if (crnbr<bestnbr)bestnbr=crnbr;
pos2=search_position(c2,pos2);
}while((pos2.x)!=-1);
pos1=search_position(c1,pos1);
}while((pos1.x)!=-1);
numbre_strokes+=bestnbr;
c1=c2;
pos1.x=0;
pos1.y=0;
pos2=pos1;
}
}
cout<<"The minimum numbre of strokes for the word entered is :"<<numbre_strokes<<endl;
system("PAUSE");
}
|