Advertisement
gabi11

Algorithms - 6. 8 Queens Puzzle

Aug 31st, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace _8_Queens_Puzzle
  7. {
  8.     public class Program
  9.     {
  10.         private const int Size = 8;
  11.         static int[,] board = new int[Size, Size];
  12.  
  13.         static HashSet<int> attackedRows = new HashSet<int>();
  14.         static HashSet<int> attackedCols = new HashSet<int>();
  15.  
  16.         static void Solve(int row)
  17.         {
  18.             if (row == Size)
  19.             {
  20.                 PrintSolution();
  21.                 return;
  22.             }
  23.  
  24.             else
  25.             {
  26.                 for (int col = 0; col < Size; col++)
  27.                 {
  28.                     if (CanPlaceQueen(row, col))
  29.                     {
  30.                         MarkAttackedFields(row, col);
  31.                         Solve(row + 1);
  32.                         UnmarkAttackedFields(row, col);
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.  
  38.         private static void PrintSolution()
  39.         {
  40.             for (int row = 0; row < Size; row++)
  41.             {
  42.                 for (int col = 0; col < Size; col++)
  43.                 {
  44.                     if (board[row, col] == 1)
  45.                     {
  46.                         Console.Write("* ");
  47.                     }
  48.  
  49.                     else
  50.                     {
  51.                         Console.Write("- ");
  52.                     }
  53.                 }
  54.                 Console.WriteLine();
  55.             }
  56.             Console.WriteLine();
  57.         }
  58.  
  59.         private static void UnmarkAttackedFields(int row, int col)
  60.         {
  61.             board[row, col] = 0;
  62.             attackedRows.Remove(row);
  63.             attackedCols.Remove(col);
  64.  
  65.         }
  66.  
  67.         private static void MarkAttackedFields(int row, int col)
  68.         {
  69.             board[row, col] = 1;
  70.             attackedRows.Add(row);
  71.             attackedCols.Add(col);
  72.         }
  73.  
  74.         private static bool CanPlaceQueen(int row, int col)
  75.         {
  76.             if (attackedRows.Contains(row))
  77.             {
  78.                 return false;
  79.             }
  80.  
  81.             if (attackedCols.Contains(col))
  82.             {
  83.                 return false;
  84.             }
  85.  
  86.             //left-up
  87.             for (int i = 1; i < Size; i++)
  88.             {
  89.                 int currentRow = row - i;
  90.                 int currentCol = col - i;
  91.  
  92.                 if (currentRow < 0 || currentRow >= Size
  93.                     || currentCol < 0 || currentCol >= Size)
  94.                 {
  95.                     break;
  96.                 }
  97.  
  98.                 //queen here
  99.                 if (board[currentRow, currentCol] == 1)
  100.                 {
  101.                     return false;
  102.                 }
  103.             }
  104.  
  105.             //right-up
  106.             for (int i = 1; i < Size; i++)
  107.             {
  108.                 int currentRow = row - i;
  109.                 int currentCol = col + i;
  110.  
  111.                 if (currentRow < 0 || currentRow >= Size
  112.                     || currentCol < 0 || currentCol >= Size)
  113.                 {
  114.                     break;
  115.                 }
  116.  
  117.                 if (board[currentRow, currentCol] == 1)
  118.                 {
  119.                     return false;
  120.                 }
  121.             }
  122.  
  123.             //left-down
  124.             for (int i = 1; i < Size; i++)
  125.             {
  126.                 int currentRow = row + i;
  127.                 int currentCol = col - i;
  128.  
  129.                 if (currentRow < 0 || currentRow >= Size
  130.                     || currentCol < 0 || currentCol >= Size)
  131.                 {
  132.                     break;
  133.                 }
  134.  
  135.                 if (board[currentRow, currentCol] == 1)
  136.                 {
  137.                     return false;
  138.                 }
  139.             }
  140.  
  141.             //right-down
  142.             for (int i = 1; i < Size; i++)
  143.             {
  144.                 int currentRow = row + i;
  145.                 int currentCol = col + i;
  146.  
  147.                 if (currentRow < 0 || currentRow >= Size
  148.                     || currentCol < 0 || currentCol >= Size)
  149.                 {
  150.                     break;
  151.                 }
  152.  
  153.                 if (board[currentRow, currentCol] == 1)
  154.                 {
  155.                     return false;
  156.                 }
  157.             }
  158.  
  159.             return true;
  160.         }
  161.  
  162.         static void Main(string[] args)
  163.         {
  164.             Solve(0);
  165.         }
  166.  
  167.  
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement