Blue Bridge Cup - Fill in the number of squares

"Sudoku" is the hottest intellectual game at the moment. It is generally believed that its origin is the "Latin square", which was invented by the great mathematician Euler in 1783.
    As shown in Figure [1.jpg]: The 6x6 grid is divided into 6 parts (distinguished by different colors in the figure), and each part contains 6 small grids (hereinafter also referred to as groups).
    At the beginning, some cells were filled with letters (one of ABCDEF). All remaining cells need to be filled with letters.
    After filling in all, the following constraints must be met:

    1. Only one of the letters A, B, C, D, E and F is allowed to be filled in.

    2. In the 6 cells of each row, the letters filled in cannot be repeated.

    3. The letters in the 6 cells in each column cannot be repeated.

    4. In the 6 cells included in each group (refer to the different colors in the figure), the letters filled in cannot be repeated.

    For the convenience of presentation, we use the following 6th-order square matrix to represent the grouping situation corresponding to Figure [1.jpg] (group numbers are 0~5):
000011
022013
221113
243333
244455
445555
 
    Use the following data to represent its existing letters Fill in:
02C
03B
05A
20D
35E
53F

    It is obvious that the first column indicates the row number, the second column indicates the column number, and the third column indicates the filled-in letter. Row and column numbers are counted from 0.

    A feasible filling scheme (this question happens to be the only answer) is:

EFCBDA
ACEDFB
DABECF
FBDCAE
BDFAEC
CEAFBD

    Your task is to write a program to solve the general Latin square problem, and if there are multiple solutions, find all the solutions.

[Input and output format requirements]

    The user first inputs 6 lines of data, indicating the grouping of Latin squares.

   Then the user enters an integer n (n<36), which represents the number of next data lines and

  then inputs n lines of data, each line representing a pre-filled letter.

    The program then outputs all possible solutions (the order of the solutions does not matter).

    Each solution occupies 7 lines.

    That is, first output an integer representing the serial number of the solution (starting from 1), and then output a 6x6 letter square matrix representing the solution.

    The letters of the solution are separated by spaces.

    If no solution that satisfies the condition is found, output "no solution".

    For example: user input:
000011
022013
221113
243333
244455
445555
6
02C
03B
05A
20D
35E
53F


    , then the program output:
1
E FCBDA
ACEDFB
DABECF
FBDCAE
BDFAEC
CEAFBD

   another example, the user input:
001111
002 113
022 243
022 443
544 433
555553
. 7
04B
05A
13D
. 14C
24E
5OC
51A
    of the program outputs:
. 1
D CEFBA
EFADCB
ABFCED
BEDAFC
FDCBAE
CABEDF
2
D CEFBA
EFADCB
ADFBEC
BECAFD
FBDCAE
CABEDF
. 3
D CFEBA
AEBDCF
FDACEB
BFEADC
EBCFAD
CADBFE
4
D C F E B A
B E A D C F
A D C F E B
F B E A D C
E F B C A D
C A D B F E
5
D C F E B A
E F A D C B
A B C F E D
B E D A F C
F D B C A E
C A E B D F
6
D C F E B A
E F A D C B
A B D F E C
B E C A F D
F D B C A E
C A E B D F
7
D C F E B A
E F A D C B
A D B F E C
B E C A F D
F B D C A E
C A E B D F
8
D C F E B A
F E A D C B
A D B C E F
B F E A D C
E B C F A D
C A D B F E
9
D C F E B A
F E A D C B
A F C B E D
B D E A F C
E B D C A F

C A B F D E

Above:


Ideas:

Deep search and backtracking, similar to the Eight Queens, is very clever when checking.

Code:

#include<bits/stdc++.h>


using namespace std;
const int maxn = 6;
const char Sodu[maxn+1] = "ABCDEF";


char m[maxn][maxn], maze[maxn][maxn];
int t, k;


bool check(int x, int y, char c)
{
    for(int i = 0; i < maxn; i++)
    {
        if(maze[i][y] == c || maze[x][ i] == c)//Detect row and column
        {
            return false;
        }
        for(int j = 0; j < maxn; j++)
        {
            if(m[i][j] == m[x][y]) // Violent search for homologous blocks
            // error if equal
            {
                if(maze[i][j] == c)
                {
                    return false;
                }
            }
        }
    }
    return true;
}


void disp()
{
    for(int i = 0; i < maxn; i++)
    {
        for(int j = 0; j < maxn; j++)
        {
            cout << maze[i][j] << " ";
        }
        cout << endl;
    }
}


void dfs(int x, int y)
{
    if(x == maxn && y == 0)
    {
        cout << ++t << endl;
        disp();
        return;
    }
    if(maze[x][y] == 0)
    {
        for(int i = 0;i < 6;i++)
        {
            if(check(x, y, Sodu[i]))
            {
                maze[x][y] = Sodu[i];
                if(y == maxn - 1)
                {
                    dfs(x + 1, 0);
                }
                else
                {
                    dfs(x, y + 1);
                }
                maze[x][y] = 0;//回溯
            }
        }
    }
    else
    {
        if(y == maxn - 1)
        {
            dfs(x + 1, 0);
        }
        else
        {
            dfs(x, y + 1);
        }
    }
}


int main()
{
    string s;
    t = 0;
    memset(maze, 0, sizeof maze);
    for(int i = 0;i < 6;i++)
    {
        cin >> s;
        for(int j = 0;j < 6;j++)
        {
            m[i][j] = s[j];
        }
    }
    cin >> k;
    while(k--)
    {
        cin >> s;
        maze[s[0] - '0'][s[1] - '0'] = s[2];
    }
    dfs(0, 0);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324510615&siteId=291194637