C#更新二维数组中的值

时间:2017-09-10 04:23:06

标签: c# arrays

我将八皇后解决方案从Java转换为C#只是为了让我更好地了解C#。问题是我在电路板上改变了价值观。我可以更改函数内部的值,但即使我使用ref,它也会立即返回到它的状态。目前我使用全局变量而不是将董事会作为参数传递。

以下是代码:

public static int[,] solution;

    static void Main()
    {
        int n = 8;
        solution = new int[8,8];

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                solution[i,j] = 0;
            }
        }
        Solve(n);
        Console.ReadKey();
    }

 public static void Solve(int n)
    {
        if (placeQueens(0, n))
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(" " + solution[i,j]);
                }
                Console.WriteLine();
            }
        }
        else
        {
            Console.WriteLine("No possible solution");
        }
    }

public static bool placeQueens(int queen, int n)
    {
        if (queen == n)
        {
            return true;
        }
        for (int row = 0; row < n; row++)
        {
            if (canPlace(row, queen))
            {

                solution[row,queen] = 1;

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        Console.Write(" " + solution[i, j]);
                    }
                    Console.WriteLine();
                }

                //When I print the current board here the 1 is set
                //When placeQueens is called again it is back to being 0
                if (placeQueens(queen + 1, n))
                {
                    return true;
                }

                solution[row,queen] = 0;
            }
        }
        return false;
    }

public static bool canPlace(int row, int col)
{ //Just checks that the position is legal}

一旦我在地方改变了它,我该怎样做才能使1保持原状?

1 个答案:

答案 0 :(得分:0)

我将静态变量更改为在每个方法中传递而不是静态变量,并且它完美地运行:

static void Main()
{
    int n = 8;
    var solution = new int[8, 8];

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            solution[i, j] = 0;
        }
    }
    Solve(solution, n);
}

public static void Solve(int[,] solution, int n)
{
    if (placeQueens(solution, 0, n))
    {
        Console.WriteLine();
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                Console.Write(" " + solution[i, j]);
            }
            Console.WriteLine();
        }
    }
    else
    {
        Console.WriteLine("No possible solution");
    }
}

public static bool placeQueens(int[,] solution, int queen, int n)
{
    if (queen == n)
    {
        return true;
    }
    for (int row = 0; row < n; row++)
    {
        if (canPlace(solution, row, queen))
        {
            solution[row, queen] = 1;

            Console.WriteLine();
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(" " + solution[i, j]);
                }
                Console.WriteLine();
            }

            if (placeQueens(solution, queen + 1, n))
            {
                return true;
            }

            solution[row, queen] = 0;
        }
    }
    return false;
}

public static bool canPlace(int[,] solution, int row, int col)
{
    for (var dx = -1; dx <= 1; dx++)
    {
        for (var dy = -1; dy <= 1; dy++)
        {
            if (dx == 0 && dy == 0)
                continue;

            int r = row, c = col;

            while (r >= 0 && r <= solution.GetUpperBound(0) && c >= 0 && c <= solution.GetUpperBound(1))
            {
                if (solution[r, c] == 1)
                    return false;

                r += dy;
                c += dx;
            }
        }
    }
    return true;
}

它输出的最后一行是一个正确的解决方案:

 1 0 0 0 0 0 0 0
 0 0 0 0 0 0 1 0
 0 0 0 0 1 0 0 0
 0 0 0 0 0 0 0 1
 0 1 0 0 0 0 0 0
 0 0 0 1 0 0 0 0
 0 0 0 0 0 1 0 0
 0 0 1 0 0 0 0 0