P1274 magic numbers game

Title Description

Filled squares digital game there are many variations, 4 × 4 square as shown below, we want to choose the numbers 1 to 16 to fill the sixteen lattice (Aij of, where i = 1..4, j = 1..4). To make the game more challenging, we require each of the following four lattice designated six of its digital accumulate and must be 34:

Numbers on the four corners, i.e., A11 + A14 + A41 + A44 = 34.

Number of squares on each corner of the 2 × 2, for example, the upper left corner: A11 + A12 + A21 + A22 = 34.

Number of squares in the middle of the most 2 × 2, i.e., A22 + A23 + A32 + A33 = 34.

Each horizontal line of four digital grid, i.e. Ai1 + Ai2 + Ai3 + Ai4 = 34, where i = 1..4.

Each vertical line number four grid, i.e. A1j + A2j + A3j + A4j = 34, where j = 1..4.

Four numbers on the two diagonals in the lattice, for example, left to bottom right: A11 + A22 + A33 + A44 = 34.

Top right to bottom left: A14 + A23 + A32 + A41 = 34

Input Format

Enter the file number 1 specifies the first fixed in a certain format. The input file contains only two positive data line I and J, and the first row represents a lattice discharge J-th column number 1. The remaining fifteen lattice, follow the aforementioned six conditions used to fill the numbers 2-16.

Output Format

Put all the correct answers with each set of four lines written to the output file, each row of four numbers, separated by a space between two adjacent numbers. Between the two sets of answers to a blank line to be white, and lined up in sequence. Sort of way, is to start with the first line start comparing numbers, each row of numbers, beginning by the left-most digit ratio, a lower number of answers must be output to a file.

Sample input and output

Input # 1
1 1
Output # 1
13 is 16. 1. 4 
14. 3 2 15 
. 8 12 is. 9. 5 
. 11. 6 10. 7 

. 1. 4 13 is 16 
14. 3 15 2 
12 is. 9. 8. 5 
. 7. 6. 11 10 


first in order to facilitate clear that we want to calculate a two coordinate grid pressed into an integer .
The specific calculation method (I, J) ( I , J ) is converted to (. 1-I) \ + J-times4. 1 ( I - . 1 ) × . 4 + J - . 1.

Then, playing a publication. . . . . .

Okay, end up.

#include <cstdio>

int b[15][4] = {
    {0, 1, 2, 3},
    {4, 5, 6, 7},
    {8, 9, 10, 11},
    {12, 13, 14, 15},
    {0, 4, 8, 12},
    {1, 5, 9, 13},
    {2, 6, 10, 14},
    {3, 7, 11, 15},
    {0, 5, 10, 15},
    {3, 6, 9, 12},
    {5, 6, 9, 10},
    {0, 1, 4, 5},
    {2, 3, 6, 7},
    {8, 9, 12, 13},
    {10, 11, 14, 15}
};

int a[16];
int vis[17];
int xz[16][10];
int cnt[16];
int pos1;

void DFS(int num){
    if (num == 16){
        if (a[pos1] != 1) return ;
        for (int i = 0; i < 4; ++i) printf("%d%c", a[i], ((i == 3) ? '\n' : ' '));
        for (int i = 4; i < 8; ++i) printf("%d%c", a[i], ((i == 7) ? '\n' : ' '));
        for (int i = 8; i < 12; ++i) printf("%d%c", a[i], ((i == 11) ? '\n' : ' '));
        for (int i = 12; i < 16; ++i) printf("%d%c", a[i], ((i == 15) ? '\n' : ' '));
        puts("");
        return ;
    }
    a[num] = 0;
    for (int i = 0; i < cnt[num]; ++i){
        if (!a[num]){
            a[num] = 34 - a[b[xz[num][i]][0]] - a[b[xz[num][i]][1]] - a[b[xz[num][i]][2]];
            if (a[num] > 16 || a[num] < 1) return ;
            if (a[num] == 1 && num != pos1) return ;
            if(force [a [whether]]) return ; 
        } 
        Else {
             if (a [whether]! = 34 - a [b [xx [whether] [i]] [ 0 ]] - a [b [xx [whether] [i]] [ 1 ]] - a [b [xx [whether] [i]] [ 2 ]]) return ; 
        } 
    } 
    If (! A [whether]) {
         if (num == Pos1) {
             if (the [ 1 ]) return ; 
            to [ 1 ] = 1 ; A [num] = 1 ; 
            DFS (whether + 1 );
            vis[1] = 0;
        }
        else{
            for (int i = 2; i <= 16; ++i){
                if(vis[i]) continue;
                vis[i] = 1; a[num] = i;
                DFS(num + 1);
                vis[i] = 0;
            }
        }
    }
    else{
        if (num == pos1 && a[num] != 1) return ;
        vis[a[num]] = 1;
        DFS(num + 1);
        vis[a[num]] = 0;
    }
}

int main(){
    int x, y; scanf("%d %d", &x, &y); pos1 = (x - 1) * 4 + y - 1;
    for (int i = 0; i < 15; ++i){
        xz[b[i][3]][cnt[b[i][3]]++] = i;
    }
    DFS(0);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11566966.html