This is a question about the C language horse dust problem.

Asked 2 weeks ago, Updated 2 weeks ago, 2 views

In order to make a single even horse defense (even number, not multiple of 4), you have to divide the horse defense into four equal parts and make four odd horse defense and fill it with the odd horse defense in the first quadrant in order of fourth, second, and third quadrants. So I wrote a code that could repeat the generation of odd magenta four times.

void SnglyEvenMagicSquare(int nMagicSqr[][N], int nN)
{
    static int nOrg[][2] = { {0, 0}, {1, 1}, {0, 1}, {1, 0} };
    int nH = nN / 2;
    int nSqr = nH * nH;
    for (int i = 0; i < 4; i++)
        OddMagicSquare(nMagicSqr, nH, nOrg[i][0] * nH, nOrg[i][1] * nH, i * nSqr + 1);
}

Let me explain the code.

void OddMagicSquare(int nMagicSqr[][N], int nN, int nOrgRow, int nOrgCol, int nBgn)
{
    int nRow, nCol, nCell = nN * nN;

    nMagicSqr[nRow = nOrgRow][nCol = nOrgCol + nN / 2] = nBgn;
    for (int i = 1; i < nCell; i++)
    {
        int nPrvRow = nRow, nPrvCol = nCol;
        nRow = (nRow - 1 + nN) % nN;
        nCol = (nCol + 1) % nN

        if (nMagicSqr[nRow][nCol]) {
            nRow = (nPrvRow + 1) % nN;
            nCol = nPrvCol;
        }

        nMagicSqr[nRow][nCol] = nBgn + i;
    }
}

Odd magenta generation code.

It's an incomplete code and it's a problem here. Odd horse-drawn is known as moving northeast from where it was first filled to fill the number, and if it is already filled in the northeast, moving south from the existing number to the northeast and filling the number. However, because it fills the quadrant, if it's 6 x 6 magenta, it fills up the nMagicSqr[0][1], and if you move northeast, you have to move to the nMagicSqr[2][2] and fill it up, and the coordinates are jumbled up. How do I modify the code under the for statement to get the desired result?

The result that we want to get is this. The solid line in the middle represents the number of 10 x 10 magenta divided into 4 for readability.

//  17  24   1   8  15 | 67  74  51  58  65
//  23   5   7  14  16 | 73  55  57  64  66
//   4   6  13  20  22 | 54  56  63  70  72
//  10  12  19  21   3 | 60  62  69  71  53
//  11  18  25   2   9 | 61  68  75  52  59
// --------------------+-------------------
//  92  99  76  83  90 | 42  49  26  33  40
//  98  80  82  89  91 | 48  30  32  39  41
//  79  81  88  95  97 | 29  31  38  45  47
//  85  87  94  96  78 | 35  37  44  46  28
//  86  93 100  77  84 | 36  43  50  27  34

c

2022-09-20 11:04

1 Answers

void OddMagicSquare(int nMagicSqr[][N], int nN, int nOrgRow, int nOrgCol, int nBgn)
{
    int nRow, nCol, nCell = nN * nN;

    nRow = 0;
    nCol = nN/2;

    nMagicSqr[nRow + nOrgRow][nCol + nOrgCol] = nBgn;
    for (int i = 1; i < nCell; i++)
    {
        int nPrvRow = nRow, nPrvCol = nCol;
        nRow = (nRow - 1 + nN) % nN;
        nCol = (nCol + 1) % nN

        if (nMagicSqr[nRow + nOrgRow][nCol + nOrgCol]) {
            nRow = (nPrvRow + 1) % nN;
            nCol = nPrvCol;
        }

        nMagicSqr[nRow + nOrgRow][nCol + nOrgCol] = nBgn + i;
    }
}

If you debug it carefully, anyone will be able to solve it.


2022-09-20 11:04

If you have any answers or tips


© 2022 pinfo. All rights reserved.