Problem :-
Coding Ninjas has provided you a crossword of 10*10 grid. The grid contains '+' or '-' as its cell values. Now, you are also provided with a word list that needs to placed accurately in the grid. Cells marked with '-' are to be filled with word list.
For example, The following is an example for the input crossword grid and the word list.
Output for the given input should be:
Note: We have provided such test cases that there is only one solution for the given input.
Input format:
Output format:
Constraints
Sample Input 1:
Sample Output 1:
Solution:-
#include<iostream>
using namespace std;
char crossWord[10][10];
bool isValidHorizontal(int row, int col, string word){
if(10 - col < word.length())
return false;
for (int i = 0, j = col; i < word.length(); ++i,j++)
{
if (crossWord[row][j] != '-' && crossWord[row][j] != word[i]){
return false;
}
}
return true;
}
bool isValidVertical(int row, int col, string word){
if(10 - row < word.length())
return false;
for (int i = row, j = 0; j < word.length(); ++i,j++)
{
if (crossWord[i][col] != '-' && crossWord[i][col] != word[j]){
return false;
}
}
return true;
}
void setHorizontal(int row, int col, string word, bool state[]){
for (int i = 0, j = col; i < word.size(); ++i, j++)
{
if (crossWord[row][j] != '+'){
if(crossWord[row][j] == word[i])
state[i] = false;
else
state[i] = true;
crossWord[row][j] = word[i];
}
}
}
void setVertical(int row, int col, string word, bool state[]){
for (int i = 0, j = row; i < word.size(); ++i, j++)
{
if (crossWord[j][col] != '+'){
if(crossWord[j][col] == word[i])
state[i] = false;
else
state[i] = true;
crossWord[j][col] = word[i];
}
}
}
void resetHorizontal(int row, int col, bool state[], int size){
for (int i = 0, j = col; i < size; ++i,j++)
{
if(state[i] == true)
crossWord[row][j] = '-';
}
return;
}
void resetVertical(int row, int col, bool state[], int size){
for (int i = 0, j = row; i < size; ++i,j++)
{
if(state[i] == true)
crossWord[j][col] = '-';
}
return;
}
void set_value(bool helper[],int len ){
for(int i=0;i<len;i++){
helper[i] = false;
}
}
bool crossWordHelper(string input[], int size, int index){
if(index == size){
for(int i =0; i<10; i++){
for(int j=0; j<10; j++){
cout << crossWord[i][j] ;
}
cout << endl;
}
return true;
}
for(int i =0; i<10; i++){
for(int j=0; j<10; j++){
if(crossWord[i][j] == '-' || crossWord[i][j] == input[index][0]){
int length = input[index].size();
bool state[length];
set_value(state,length);
if(isValidHorizontal(i, j, input[index])){
setHorizontal(i, j, input[index], state);
if(crossWordHelper(input, size, index+1)){
return true;
}
resetHorizontal(i, j, state, length);
}
if(isValidVertical(i, j, input[index])){
setVertical(i, j, input[index], state);
if(crossWordHelper(input, size, index+1)){
return true;
}
resetVertical(i, j, state, length);
}
}
}
}
return false;
}
void solveCrossWord(string input[], int size){
bool res = crossWordHelper(input, size, 0);
return;
}
int main(){
string ss;
for(int i = 0; i<10; i++){
cin >>ss;
for(int j = 0; j < ss.size(); j++){
crossWord[i][j] = ss[j];
}
}
char s[200];
cin >> s;
string input[10];
char ch;
string word ="";
int a =0;
for (int i = 0; s[i] != '\0'; ++i)
{
if(s[i] == ';'){
input[a++] = word;
word ="";
}
else {
word += s[i];
}
}
input[a++] = word;
solveCrossWord(input, a);
return 0;
}
Recommended Post :-
HCL Coding Questions:-
Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-
- Swap the adjacent characters of the string
- Double the vowel characters in the string
- Character with their frequency
- Program to find the closest value
Must check this:-
Companies interview:-
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Full C course:-
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments