//link :-https://www.hackerrank.com/challenges/crossword-puzzle/problem
#include <bits/stdc++.h>
using namespace std;
bool check_r(int i,int j,vector &crossword,string newstr,int counter)
{
if(newstr.size()==0)
{
return true;
}
if(counter==0)
{
int x=i;
int y=j;
int z=0;
int count=0;
while((crossword[x][y]=='-'||crossword[x][y]==newstr[z]) && y<crossword[x].size())
{
z++;
y++;
count++;
}
if(count!=newstr.size())
{
return false;
}
cout<<count<<"r"<<newstr.size()<<endl;
}
while(crossword[i].size()>j&&(crossword[i][j]=='-'||crossword[i][j]==newstr[0]))
{
char c=crossword[i][j];
crossword[i][j]=newstr[0];
if(check_r(i,j+1,crossword,newstr.substr(1),counter+1))
return true;
else
{
crossword[i][j]=c;
return false;
}
}
return false;
}
bool check_b(int i,int j,vector &crossword,string newstr,int counter)
{
if(newstr.size()==0)
{
return true;
}
if(counter==0)
{
int x=i;
int y=j;
int z=0;
int count=0;
while((crossword[x][y]=='-'||crossword[x][y]==newstr[z]) && x<crossword.size())
{
z++;
x++;
count++;
}
if(count!=newstr.size())
{
return false;
}
cout<<count<<"b "<<newstr.size()<<endl;
}
while(i<crossword.size()&&(crossword[i][j]=='-'||crossword[i][j]==newstr[0]))
{
char c=crossword[i][j];
crossword[i][j]=newstr[0];
if(check_b(i+1,j,crossword,newstr.substr(1),counter+1))
return true;
else
{
crossword[i][j]=c;
return false;
}
}
return false;
}
bool solve (vector &crossword,string words)
{
if(words.size()==0)
{
for(int i=0;i<crossword.size();i++ )
{
for(int j=0;j<crossword[i].size();j++)
{ cout<<crossword[i][j];}
cout<<endl;}
//cout<<words.size()<<endl;
return true;
}
unsigned int i,j;
for(i=0;i<crossword.size();i++ )
{
for(j=0;j<crossword[i].size();j++)
{
if(crossword[i][j]=='-'||crossword[i][j]==words[0])
{
//cout<<crossword[i][j]<<endl;
int pos=words.find(';');
//cout<<pos<<endl;
string newstr=words.substr(0,pos);
//cout<<newstr<<endl;
//cout<<i<<" "<<j<<endl;
if(check_r(i,j,crossword,newstr,0))
{
if(solve(crossword,words.substr(pos+1)))
return true;
}
/* if(check_l(i,j,crossword,newstr))
{
if(solve(crossword,words.substr(pos+1)))
return true;
}
if(check_t(i,j,crossword,newstr))
{
if(solve(crossword,words.substr(pos+1)))
return true;
}*/
if(check_b(i,j,crossword,newstr,0))
{
if(solve(crossword,words.substr(pos+1)))
return true;
}
return false;
}
}
}
//return false;
}
// Complete the crosswordPuzzle function below.
vector crosswordPuzzle(vector crossword, string words) {
solve(crossword,words);
}
int main()
{
ofstream fout(getenv(“OUTPUT_PATH”));
vector<string> crossword(10);
for (int i = 0; i < 10; i++) {
string crossword_item;
getline(cin, crossword_item);
crossword[i] = crossword_item;
}
string words;
getline(cin, words);
words=words+';';
crosswordPuzzle(crossword, words);
fout << "\n";
fout.close();
return 0;
}