骑士们进行8x8无限反曲之旅

本文关键字:之旅 无限 8x8 骑士 | 更新日期: 2023-09-27 18:27:09

这将是我为著名的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;
    }
}

骑士们进行8x8无限反曲之旅

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

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