📜  解决填字游戏

📅  最后修改于: 2021-05-04 14:44:50             🧑  作者: Mango

提供了一个10 x 10填字游戏网格,以及需要填充到网格中的一组单词(或地点名称)。网格中的单元格最初是+号或号。标有“ +”的单元格必须保留原样。标有“-”的单元格需要填充适当的字符。
您还会得到需要在填字游戏网格中填充的单词数组。

例子 :

Input :
+++++++++-
-++++++++-
-------++-
-++++++++-
-++++++++-
-++++-----
------+++-
-++++++++-
+---------
++++++++++

Output :
+++++++++C
P++++++++H
HISTORY++E
Y++++++++M
S++++++++I
I++++MATHS
CIVICS+++T
S++++++++R
+GEOGRAPHY
++++++++++

其背后的方法是递归检查垂直位置和水平位置中的每个单词。然后将单词填充到最适合网格对应位置的矩阵中,然后通过用该单词填充空白来更新填字游戏网格。

// CPP code to fill the crossword puzzle
#include 
using namespace std;
  
// ways are to calculate the number of
// possible ways to fill the grid
int ways = 0;
  
// this function is used to print
// the resultant matrix
void printMatrix(vector& matrix, int n)
{
    for (int i = 0; i < n; i++)
        cout << matrix[i] << endl;
}
  
// this function checks for the current word
// if it can be placed horizontally or not
// x -> it represent index of row
// y -> it represent index of column
// currentWord -> it represent the
// current word in word array
vector checkHorizontal(int x, int y,
                               vector matrix,
                               string currentWord)
{
    int n = currentWord.length();
  
    for (int i = 0; i < n; i++) {
        if (matrix[x][y + i] == '#' || 
            matrix[x][y + i] == currentWord[i]) {
            matrix[x][y + i] = currentWord[i];
        }
        else {
  
            // this shows that word cannot 
            // be placed horizontally
            matrix[0][0] = '@';
            return matrix;
        }
    }
  
    return matrix;
}
  
// this function checks for the current word
// if it can be placed vertically or not
// x -> it represent index of row
// y -> it represent index of column
// currentWord -> it represent the
// current word in word array
vector checkVertical(int x, int y,
                             vector matrix,
                             string currentWord)
{
    int n = currentWord.length();
  
    for (int i = 0; i < n; i++) {
        if (matrix[x + i][y] == '#' || 
            matrix[x + i][y] == currentWord[i]) {
            matrix[x + i][y] = currentWord[i];
        }
        else {
  
            // this shows that word
            // cannot be placed vertically
            matrix[0][0] = '@';
            return matrix;
        }
    }
    return matrix;
}
  
// this function recursively checks for every
// word that can align vertically in one loop
// and in another loop it checks for those words
// that can align horizontally words -> it
// contains all the words to fill in a crossword
// puzzle matrix -> it contain the current
// state of crossword index -> it represent
// the index of current word n -> it represent
// the length of row or column of the square matrix
void solvePuzzle(vector& words,
                 vector matrix,
                 int index, int n)
{
    if (index < words.size()) {
        string currentWord = words[index];
        int maxLen = n - currentWord.length();
  
        // loop to check the words that can align vertically.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= maxLen; j++) {
                vector temp = checkVertical(j, i,
                                        matrix, currentWord);
  
                if (temp[0][0] != '@') {
                    solvePuzzle(words, temp, index + 1, n);
                }
            }
        }
  
        // loop to check the words that can align horizontally.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= maxLen; j++) {
                vector temp = checkHorizontal(i, j,
                                      matrix, currentWord);
  
                if (temp[0][0] != '@') {
                    solvePuzzle(words, temp, index + 1, n);
                }
            }
        }
    }
    else {
        // calling of print function to
        // print the crossword puzzle
        cout << (ways + 1) << " way to solve the puzzle "
             << endl;
        printMatrix(matrix, n);
        cout << endl;
  
        // increase the ways
        ways++;
        return;
    }
}
  
// Driver Code
int main()
{
    // length of grid
    int n1 = 10;
  
    // matrix to hold the grid of puzzle
    vector matrix;
  
    // take input of puzzle in matrix
    // input of grid of size n1 x n1
    matrix.push_back("*#********");
    matrix.push_back("*#********");
    matrix.push_back("*#****#***");
    matrix.push_back("*##***##**");
    matrix.push_back("*#****#***");
    matrix.push_back("*#****#***");
    matrix.push_back("*#****#***");
    matrix.push_back("*#*######*");
    matrix.push_back("*#********");
    matrix.push_back("***#######");
  
    vector words;
  
    // the words matrix will hold all
    // the words need to be filled in the grid
    words.push_back("PUNJAB");
    words.push_back("JHARKHAND");
    words.push_back("MIZORAM");
    words.push_back("MUMBAI");
  
    // initialize the number of ways
    // to solve the puzzle to zero
    ways = 0;
  
    // recursive function to solve the puzzle
    // Here 0 is the initial index of words array
    // n1 is length of grid
    solvePuzzle(words, matrix, 0, n1);
    cout << "Number of ways to fill the grid is "
         << ways << endl;
  
    return 0;
}
输出:
1 way to solve the puzzle 
*J********
*H********
*A****P***
*R#***U#**
*K****N***
*H****J***
*A****A***
*N*MUMBAI*
*D********
***MIZORAM

Number of ways to fill the grid is 1