2014-10-19 66 views
-1

這裏將是我的代碼爲8x8甲板的着名騎士之旅。所以,我的代碼的主要思想是:我們將選擇打開我們的目的地,用isPossible檢查它,然後對其進行檢查,將此單元格標記爲1。所以,檢查每個細胞,如果我們將在64個細胞中 - 返回true。 但我的代碼去無限recurssion,我無法調試它,任何建議將不勝感激。騎士之旅爲8x8無限遞歸

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; 
    } 
} 
+3

「我無法調試它」爲什麼不呢?我建議你學習如何調試它。 – spender 2014-10-19 21:49:43

+0

但是這裏是所有變體的半身像,我該如何調試它? – ratkke 2014-10-19 21:58:21

+1

您可以使用斷點輕鬆地使用Visual Studio調試器進行調試,甚至只需將某些跟蹤輸出放在正確的位置,並確保所有值都在預期的範圍內。 – spender 2014-10-19 22:01:44

回答

3

我認爲你的問題是你的測試計數< 64,但你從來沒有分配數。你只是通過(通過值!)'計數+1'給put方法。您可能認爲這將寫回計數變量。但事實並非如此......請注意,調試是您需要學習的第一項技能!

+0

謝謝你的建議,但是當我改變了'count + 1'到'++計數'這個程序實際上適用於8x8。但它也適用於4x4,這是不可能的。 – ratkke 2014-10-19 22:25:49

+0

@ratkke你的時間複雜度是多少?嘗試每個8×8(這是64!)的排列對於任何普通計算機來說都是不可能的,所以如果這適用於4x4(16!)但不是8x8 – 2014-10-20 05:58:32

+1

是正常的您的問題是爲什麼您的代碼無限期地運行。我指出了你。爲什麼它也適用於4x4 ....?我想你應該看看你的設計... – 2014-10-20 12:14:55