SHARE
TWEET

EightQueens

a guest Aug 29th, 2014 208 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class EightQueensObjects
  5. {
  6.     static char[,] field = {
  7.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  8.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  9.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  10.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  11.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  12.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  13.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  14.                                 { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
  15.                            };
  16.  
  17.     private static int numberOfQueens = 8;
  18.     static int foundSolutions = 0;
  19.  
  20.     static void Main()
  21.     {
  22.         int column = 0;
  23.  
  24.         for (int row = 0; row < 8; row++)
  25.         {
  26.             //here is created the first queen
  27.             Queen currentQueen = new Queen(column, row, null);
  28.             PutQueens(currentQueen);
  29.         }
  30.     }
  31.  
  32.     //this recursive method will put every new queen on the field
  33.     static void PutQueens(Queen currentQueen)
  34.     {
  35.         if (currentQueen.Column == numberOfQueens)
  36.         {
  37.             PrintField();
  38.             foundSolutions++;
  39.             return;
  40.         }
  41.  
  42.         //put the new queen on the field
  43.         field[currentQueen.Row, currentQueen.Column] = 'Q';
  44.         Queen nextQueen;
  45.  
  46.         for (int rows = 0; rows < field.GetLength(0); rows++)
  47.         {
  48.             bool isChecked = false;
  49.             Queen queenBefore = currentQueen;
  50.             //check if we can put the new queen here
  51.             while (rows != queenBefore.Row &&
  52.                 Math.Abs(rows - queenBefore.Row) != Math.Abs(currentQueen.Column + 1 - queenBefore.Column)) //check if the new queen will be attacked diagonally
  53.             {
  54.                 isChecked = true;
  55.  
  56.                 if (queenBefore.Parent != null)
  57.                 {
  58.                     queenBefore = queenBefore.Parent;
  59.                 }
  60.                 else
  61.                 {
  62.                     break;
  63.                 }
  64.             }
  65.  
  66.             //if the new queen is not threatened by all the queens, we can put it here
  67.             if (queenBefore.Column == 0 && isChecked)
  68.             {
  69.                 nextQueen = new Queen(currentQueen.Column + 1, rows, currentQueen);
  70.                 PutQueens(nextQueen);
  71.             }
  72.         }
  73.  
  74.         field[currentQueen.Row, currentQueen.Column] = ' ';
  75.     }
  76.  
  77.     //this method will print the current solution
  78.     static void PrintField()
  79.     {
  80.         for (int row = 0; row < field.GetLength(0); row++)
  81.         {
  82.             for (int col = 0; col < field.GetLength(1); col++)
  83.             {
  84.                 if (field[row, col] == 'Q')
  85.                     Console.Write(field[row, col].ToString().PadLeft(3));
  86.                 else
  87.                     Console.Write("*".PadLeft(3));
  88.             }
  89.             Console.WriteLine();
  90.         }
  91.         Console.WriteLine(foundSolutions);
  92.         Console.WriteLine();
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top