-1

这将是我著名的 8x8 甲板骑士之旅的代码。所以,我的代码的主要思想是:我们将从 Turns 中选择我们的目的地,用 isPossible 检查它,然后递归到它,将此单元格标记为“1”。所以,检查每个单元格,如果我们将在 64 的单元格中 - 返回 true。但是我的代码会无限递归,我无法调试它,任何建议都将不胜感激。

class Class1
{
    static void Main(string[] args)
    {
        int x = 0;
        int y = 0;
        Console.WriteLine("Enter X and press enter");
        x = Int32.Parse(Console.ReadLine());
        Console.WriteLine("Enter Y and press enter");
        y = Int32.Parse(Console.ReadLine());
        TurnVariation Turns = new TurnVariation(); 
        EmptyBoard Board = new EmptyBoard();
        if (TryPut.Put(Board, x, y, Turns, 1, false))
        {
            Console.WriteLine("МОЖНА!!!!");
        }
        else
        {
            Console.WriteLine("NET!!");
        }
    }
}

public class TryPut : EmptyBoard
{
    public static bool Put(EmptyBoard Board, int x, int y, TurnVariation Turns, int count, bool flag)
    {
        int tempX = 0;
        int tempY = 0;
        if (count >= 64)
        {
            Console.WriteLine("yeab");
            return true;
        }
        for (int i = 0; i <= 7; i++)
        {
            tempX = x + Turns.Turns[i,0];
            tempY = y + Turns.Turns[i,1];
            //Console.WriteLine(count); 
            if (IsPossible(Board, tempX, tempY))
            {
                Board.Array[tempX, tempY] = 1;

                flag = Put(Board, tempX, tempY, Turns, count+1, flag);
                if (flag)
                {
                    break;
                }
                Board.Array[tempX, tempY] = 0;
            }
        }
        if (flag)
            return true;
        else
            return false;
    }

    public static bool IsPossible(EmptyBoard Board, int x, int y)
    {
        if ((x < 0) || (x > 7) || (y < 0) || (y > 7))
            return false;
        if (Board.Array[x, y] == 1)
            return false;
        return true;
    }
}

public class TurnVariation
{
    public int[,] Turns = new int[8, 2];
    public TurnVariation()
    {
        Turns[0, 0] = -2; Turns[0, 1] = 1;
        Turns[1,0] = -2; Turns[1,1] = -1;
        Turns[2,0] = -1; Turns[2,1] = 2;
        Turns[3,0] = 1; Turns[3,1] = 2;
        Turns[4,0] = 2; Turns[4,1] = 1;
        Turns[5,0] = 2; Turns[5,1] = -1;
        Turns[6,0] = 1; Turns[6,1] = -2;
        Turns[7,0] = -1; Turns[7,1] = -2;
    }
}

public class EmptyBoard
{
    public const int N = 8;
    public int[,] Array = new int[N, N];
    public EmptyBoard()
    {
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                Array[i, j] = 0;
    }
}
4

1 回答 1

3

我认为您的问题是您对 count<64 的测试,但您从未分配给 count。您只需将(按值!)'Count + 1' 传递给 put 方法。您可能认为这将被写回 count 变量。但事实并非如此...

请注意,调试是您需要学习的第一项技能!

于 2014-10-19T22:00:07.403 回答