Tuesday, March 30, 2021

knights tour

 using System;

using System.Linq;

using System.Collections.Generic;


public class Program

{

static int[] rowArray = new int[8] {2, 2, -2, -2, -1, 1, -1, 1};

static int[] colArray = new int[8] {-1, 1, -1, 1, 2, 2, -2, -2};

//static int[] rowArray = new int[8]{ 2, 1, -1, -2, -2, -1, 1, 2 };

//

    

//static int[] colArray = new int[8] { 1, 2, 2, 1, -1, -2, -2, -1 };

private static bool[,] knights = new bool[8, 8];

private static List<string> placedKnightsCount = new List<string>();

public static void Main()

{

setKnightToFalse();

placedKnightsCount.Add("0,0");

knights[0,0]=true;

placeKnights(0,0);

int index=1;

foreach(var move in placedKnightsCount)

{

System.Console.WriteLine(index.ToString()+"::"+move.ToString());

index++;

}

System.Console.WriteLine(placedKnightsCount.Select(x=>x).Count());

}

private static bool  placeKnights(int row, int col)

{

if (placedKnightsCount.Count() >= 64)

           return true;

  for (var array = 0; array <= 7; array++) 

{

      if (rowArray[array] + row >= 0 && rowArray[array] + row <= 7

        && colArray[array] + col >= 0 && colArray[array] + col <= 7 &&

        knights[rowArray[array] + row,colArray[array] + col] == false) 

{

knights[rowArray[array] + row,colArray[array] + col] = true;

placedKnightsCount.Add((rowArray[array] + row).ToString() + ',' + (colArray[array] + col).ToString()

   +"::rowArray"+rowArray[array].ToString()+","+row.ToString()

  +"::cola=Array "+colArray[array].ToString()+","+col.ToString()

+","+"::cola=sum "+(colArray[array]+col).ToString()

);

var b = colArray[array] + col;

if (placeKnights(rowArray[array] + row,b))

{

 

return true;

}

else 

{

knights[rowArray[array] + row,colArray[array] + col] = false;

System.Console.WriteLine("FAILED::rowArray"+rowArray[array].ToString()+","+row.ToString()

  +"::cola=Array "+colArray[array].ToString()+","+col.ToString());

}

  }

}

return false;

}

private  static void setKnightToFalse() {

    for (var row = 0; row <= 7; row++) {

      //this.knights[row] = [];//

      for (var col = 0; col <= 7; col++) {

       

        knights[row,col] = false;

      }

    }

   //System.Console.Write(knights[7,7]);


  }

  


}


 

No comments:

Post a Comment