0

对于一个介绍程序,我们被要求构建一个程序,该程序可以找到给定大小的每个可能的工作幻方。我无法从递归函数中修改类变量。每次我尝试的数字组合产生一个幻方时,我都试图增加找到的幻方的数量。

更具体地说,我正在尝试修改函数 recursiveMagic() 中的 numSquares。在该特定行设置断点后,变量 numSquares 不会更改,即使我正在递增它。我认为这与递归有关,但是,我不确定。如果您想提供一些建议,我将不胜感激。

//============================================================================
// Name        : magicSquare.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

/**
 * MagicSquare
 */
class MagicSquare {
private:
    int magicSquare[9];
    int usedNumbers[9];
    int numSquares;
    int N;
    int magicInt;
public:

    MagicSquare() {
        numSquares = 0;
        for (int i = 0; i < 9; i++)
            usedNumbers[i] = 0;
        N = 3; //default is 3
        magicInt = N * (N * N + 1) / 2;
    }

    MagicSquare(int n) {
        numSquares = 0;
        for (int i = 0; i < 9; i++)
            usedNumbers[i] = 0;
        N = n;
        magicInt = N * (N * N + 1) / 2;
    }

    void recursiveMagic(int n) {
        for (int i = 1; i <= N * N + 1; i++) {
            if (usedNumbers[i - 1] == 0) {
                usedNumbers[i - 1] = 1;
                magicSquare[n] = i;
                if (n < N * N)
                    recursiveMagic(n + 1);
                else {
                    if (isMagicSquare()) {
                        numSquares++; //this is the line that is not working correctly
                        printSquare();
                    }
                }
                usedNumbers[i - 1] = 0;
            }
        }
    }
    //To efficiently check all rows and collumns, we must convert the one dimensional array into a 2d array
    //since the sudo 2d array looks like this:
    //        0 1 2
    //        3 4 5
    //        6 7 8
    //the following for-if loops convert the i to the appropriate location.

    bool isMagicSquare() {
        for (int i = 0; i < 3; i++) {
            if ((magicSquare[i * 3] + magicSquare[i * 3 + 1] + magicSquare[i * 3 + 2]) != magicInt) //check horizontal
                return false;
            else if ((magicSquare[i] + magicSquare[i + 3] + magicSquare[i + 6]) != magicInt) // check vertical
                return false;
        }
        if ((magicSquare[0] + magicSquare[4] + magicSquare[8]) != magicInt)
            return false;
        if ((magicSquare[6] + magicSquare[4] + magicSquare[2]) != magicInt)
            return false;
        return true;
    }

    /**
     * printSquare: prints the current magic square combination
     */
    void printSquare() {
        for (int i = 0; i < 3; i++)
            cout << magicSquare[i * 3] << " " << magicSquare[i * 3 + 1]
                << " " << magicSquare[i * 3 + 2] << endl;
        cout << "------------------" << endl;
    }

    /**
     * checkRow: checks to see if the current row will complete the magic square
     * @param i - used to determine what row is being analyzed
     * @return true if it is a working row, and false if it is not
     */
    bool checkRow(int i) {
        i = (i + 1) % 3 - 1;
        return (magicSquare[i * 3] + magicSquare[i * 3 + 1] + magicSquare[i * 3 + 2]) == magicInt;
    }

    int getnumSquares() {
        return numSquares;
    }
}; //------End of MagicSquare Class-----

int main() {
    MagicSquare square;
    cout << "Begin Magic Square recursion:" << endl << "------------------"
            << endl;
    square.recursiveMagic(0);
    cout << "Done with routine, returned combinations: " << square.getnumSquares() << endl;
    return 0;
}
4

1 回答 1

1

数组被覆盖导致覆盖 numSquares 字段。

class MagicSquare {
private:
    int magicSquare[9];
    int usedNumbers[9];

更改为

class MagicSquare {
private:
    int magicSquare[10];
    int usedNumbers[10];

同样在你的初始化程序中,循环说< 9,但你想说的是< 10。或者只是使用 memset 更好地达到这个目的。

于 2013-02-18T00:55:39.740 回答