Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
You are given a grid denoting a simple crossword, consisting of the characters '#', 'r', 'c', 'b' and '.'.

In this simple crossword, every word must be filled either from left to right (horizontally) or from top to bottom (vertically).

A cell containing '#' is a blocked cell that is supposed to be ignored (i.e. not filled with any character).

A cell containing 'r' implies that there should be at least one word filled horizontally (from left to right) either starting at or ending at this position.

A cell containing 'c' implies that there should be at least one word filled vertically (from top to bottom) either starting at or ending at this position.

A cell containing 'b' implies that there should be at least one word filled vertically (from top to bottom) either starting at or ending at this position and another word filled horizontally (from left to right) either starting at or ending at this position.

There should not be more than one horizontally filled word in a row.

There should not be more than one vertically filled word in a column.

You are also given the list of words that solve the crossword puzzle, determine a way to write the given words in the grid following the above constraints. Every word that is given should be written in the crossword.

It is guaranteed that all words given have different lengths.

It is also guaranteed that for every word to be filled, the corresponding starting and ending positions are given in the grid as 'r', 'c' or 'b'.

In case there is no solution satisfying the given conditions print "Invalid" instead.

Input
First line contains 2 space separated integers n,m denoting the number of rows and columns of the grid.

Next n lines contain m characters each denoting the grid.

A '#' denotes that the cell is blocked, i.e. contains no characters

A '.' denotes an empty cell that should be filled with some character

An 'r' denotes that there exists a word that should be filled horizontally (i.e. from left to right) starting from this position

A 'c' denotes that there exists a word that should be filled vertically (i.e. from top to bottom) starting from this position

A 'b' denotes that there exists a word that is should be filled vertically (i.e. from top to bottom) starting from this position and another word that is to be filled horizantally (i.e. from left to right) starting from this position.

The next line contains a single integer w, the number of words to be filled

The next w lines each contains a string consisting of lower case english letters, a word that is to be written on the crossword.

Output
If there is no solution then print "Invalid", otherwise the solution will always be unique.

Print n lines each containing m characters denoting the value of the cell

A '#' cell should remain as is

Every '.', 'r', 'c' and 'b' cell given in the input should be replaced by an english character from 'a' to 'z' such that it satisfies the problem constraints

Constraints: 1≤n,m≤50
Every word in the dictionary has different length and contains at least 2 characters
There is always either a unique solution satisfying the given constraints or no solution.

Sample Input
8 8
b.c..r##
.#r.r###
.#rr####
.#c#####
.#######
.#######
.#######
c#######
5
solution
sample
many
ash
no

Sample Output
sample##
o#ash###
l#no####
u#y#####
t#######
i#######
o#######
n#######

Sample Input 2
8 8
b...c..r
.###r.r#
.###rr##
.###c###
.#######
c#######
########
########
5
this
sample
has
no
solution

Sample Output 2
Invalid

Explanation
The given words are filled into the crossword as shown for the first sample input.

We can fill the second sample as follows, observe that "no" cannot be filled in the remaining positions.
solution
a###has#
m###i.##
p###s###
l#######
e#######
########
########

Hence the answer is "Invalid".

What I have tried:

C
#include <stdio.h>
#include <string.h>
#define MAX_GRID 100

int num_rows, num_cols;
char grid[MAX_GRID][MAX_GRID];
char new_grid[MAX_GRID][MAX_GRID];

int num_words;
int len_words[MAX_GRID];
char words[MAX_GRID][MAX_GRID];

int find_row(int row, int col){
    int len_word = 2;
    int col_end;
    for (col_end = col+1; grid[row][col_end] != 'r'; col_end++)
        len_word++;
    grid[row][col_end] = 'R';

    int word;
    for (word = 0; word < num_words; word++)
        if (len_word == len_words[word])
            break;
    if (word == num_words)
        return 0;

    if (new_grid[row][col] != '.'){
        if (new_grid[row][col] != words[word][0]){
            return 0;
        }
    }

    for (int i = 0; i < len_words[word]; i++)
        new_grid[row][col+i] = words[word][i];

    return 1;
}

int find_col(int row, int col){
    int len_word = 2;
    int row_end;
    for (row_end = row+1; grid[row_end][col] != 'c'; row_end++)
        len_word++;
    grid[row_end][col] = 'C';

    int word;
    for (word = 0; word < num_words; word++)
        if (len_word == len_words[word])
            break;
    if (word == num_words)
        return 0;
        
    if (new_grid[row][col] != '.'){
        if (new_grid[row][col] != words[word][0]){
            return 0;
        }
    }

    for (int i = 0; i < len_words[word]; i++)
        new_grid[row+i][col] = words[word][i];

    return 1;
}

int main(){
    scanf("%d %d", &num_rows, &num_cols);
    for (int i = 0; i < num_rows; i++)
        scanf("%s", grid[i]);

    for (int i = 0; i < num_rows; i++)
        for (int j = 0; j < num_cols; j++)
            new_grid[i][j] = '.';

    scanf("%d", &num_words);
    for (int i = 0; i < num_words; i++)
    {
        scanf("%s", words[i]);
        len_words[i] = strlen(words[i]);
    }

    int is_valid = 1;
    for (int row = 0; row < num_rows; row++){
        for (int col = 0; col < num_cols; col++){
            if (grid[row][col] == '.'){
                continue;
            }
            else if (grid[row][col] == '#'){
                new_grid[row][col] = '#';
            }
            else if (grid[row][col] == 'b'){
                if (find_row(row, col) == 0)
                    is_valid = 0;
                if (find_col(row, col) == 0)
                    is_valid = 0;
            }
            else if (grid[row][col] == 'r'){
                if (find_row(row, col) == 0)
                    is_valid = 0;
            }
            else if (grid[row][col] == 'c'){
                if (find_col(row, col) == 0)
                    is_valid = 0;
            }
        }
    }

    if (is_valid == 0)
        printf("Invalid\n");
    else if(is_valid == 1){
        for (int i = 0; i < num_rows; i++){
            for (int j = 0; j < num_cols; j++)
                printf("%c", new_grid[i][j]);
            printf("\n");
        }
    }
    return 0;
}

It runs correctly on the test cases, but when i submit it, it shows runtime error.
Posted
Updated 7-Jul-22 3:28am
v3
Comments
Richard MacCutchan 7-Jul-22 5:31am    
What runtime error, and where does it occur?
Prabhjot Singh 2022 7-Jul-22 5:51am    
SIGSEGV, i don't know the part of code that causes the error
Richard MacCutchan 7-Jul-22 7:34am    
That means that somewhere in your code you are using a bad memory address. That can happen when a pointer gets corrupted or an array index is set to a wrong value. The only way to find it is by using the debugger.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Try with starting some Learn C++ tutorial and go to Chapter 3 "Debugging C++ Programs" or some video tutorial.

Debugging code is the most time costing activity in programming. Writing some code is done in some hours, but killing all bugs is a battle of weeks.

some tips: use functions and classes with understandable names and install some IDE like Visual Studio.
 
Share this answer
 
Have checked the program with the test inputs. Neither compiler error nor crash. Is it possible that something went wrong when entering it?

Output:
Invalid
solution
a###has#
m###i.##
p###s###
l#######
e#######
########
########

Using the program is very frustrating because on the one hand a lot has to be entered and on the other hand nothing is displayed. A lot can go wrong there. So, for test I entered it directly:
C
// Sample Input2:
#define SOL2
#ifdef SOL2
	num_rows = 8; num_cols = 8; // S2
	strcpy(grid[0], "b...c..r");
	strcpy(grid[1], ".###r.r#");
	strcpy(grid[2], ".###rr##");
	strcpy(grid[3], ".###c###");
	strcpy(grid[4], ".#######");
	strcpy(grid[5], "c#######");
	strcpy(grid[6], "########");
	strcpy(grid[7], "########");

	num_words = 5;  // S2
	strcpy(words[0], "this");
	strcpy(words[1], "sample");
	strcpy(words[2], "has");
	strcpy(words[3], "no");
	strcpy(words[4], "solution");
#endif

The program could be much more user-friendly if the data is read from a file. That would also be reproducible, especially for troubleshooting.

I would also strongly recommend creating the fields dynamically so that they can be adapted to the task. It might also make sense to check the inputs.
 
Share this answer
 
v3
Comments
Prabhjot Singh 2022 7-Jul-22 12:57pm    
it works fine with the test cases, but solution is not accepted when i submit it
merano99 7-Jul-22 16:46pm    
please vote and try again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900