Advertisement
cyecize

8 Queens Puzzle

Mar 12th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.00 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace a06QueensPuzzle
  10. {
  11.     class Program
  12.     {
  13.         private static int solutions = 0;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             SolveQueensProblem(new List<Queen>(), 0);
  18.  
  19.             // Console.WriteLine(solutions);
  20.         }
  21.  
  22.         private static void SolveQueensProblem(List<Queen> queens, int startingRow)
  23.         {
  24.             Board board = new Board(queens);
  25.             System.GC.Collect(); //haha fuck you softuni I have become smarter
  26.  
  27.             if (queens.Count >= 8)
  28.             {
  29.                 solutions++;
  30.  
  31.                 board.PrintBoard();
  32.                 return;
  33.             }
  34.  
  35.             if (startingRow >= 8)
  36.                 return;
  37.  
  38. //            if (solutions == 92) //faster way for exiting the recursion now that I know the number of solutions
  39. //            {
  40. //                throw new Exception();
  41. //            }
  42.  
  43.             for (int col = 0; col < 8; col++)
  44.             {
  45.                 Point currentPoint = new Point(startingRow, col);
  46.  
  47.                 if (!board.IsPointAttacked(currentPoint))
  48.                 {
  49.                     List<Queen> newQueens = new List<Queen>(board.Queens) { new Queen(currentPoint) };
  50.  
  51.                     SolveQueensProblem(newQueens, startingRow + 1);
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.  
  58.     internal class Queen
  59.     {
  60.         private Point positionOnBoard;
  61.  
  62.         private List<Point> attackingPoints;
  63.  
  64.         public Queen(Point positionOnBoard)
  65.         {
  66.             this.PositionOnBoard = positionOnBoard;
  67.         }
  68.  
  69.         public Point PositionOnBoard
  70.         {
  71.             get => positionOnBoard;
  72.             set
  73.             {
  74.                 this.positionOnBoard = value;
  75.                 this.InitAttackingPositions();
  76.             }
  77.         }
  78.  
  79.         public bool IsAttackingPoint(Point point)
  80.         {
  81.             return this.attackingPoints.Exists(p => p.Row == point.Row && p.Col == point.Col);
  82.         }
  83.  
  84.         private void InitAttackingPositions()
  85.         {
  86.             this.attackingPoints = new List<Point>();
  87.             this.AddRowAttackingPoints();
  88.             this.AddColAttackingPoints();
  89.             this.AddDiagonalAttackingPoints();
  90.             this.attackingPoints.Add(this.PositionOnBoard);
  91.         }
  92.  
  93.         private void AddRowAttackingPoints()
  94.         {
  95.             for (var row = this.PositionOnBoard.Row + 1; row < Board.BoardSize; row++)
  96.                 this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col));
  97.  
  98.             for (var row = this.PositionOnBoard.Row - 1; row >= 0; row--)
  99.                 this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col));
  100.         }
  101.  
  102.         private void AddColAttackingPoints()
  103.         {
  104.             for (var col = this.PositionOnBoard.Col + 1; col < Board.BoardSize; col++)
  105.                 this.attackingPoints.Add(new Point(this.PositionOnBoard.Row, col));
  106.  
  107.             for (var col = this.PositionOnBoard.Col - 1; col >= 0; col--)
  108.                 this.attackingPoints.Add(new Point(this.PositionOnBoard.Row, col));
  109.         }
  110.  
  111.         private void AddDiagonalAttackingPoints()
  112.         {
  113.             //check right diagonals
  114.             var accumulatedCols = 0;
  115.             for (var row = this.PositionOnBoard.Row + 1; row < Board.BoardSize; row++)
  116.             {
  117.                 accumulatedCols++;
  118.                 if (this.PositionOnBoard.Col + accumulatedCols < Board.BoardSize)
  119.                     this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col + accumulatedCols));
  120.  
  121.                 if (this.PositionOnBoard.Col - accumulatedCols >= 0)
  122.                     this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col - accumulatedCols));
  123.             }
  124.  
  125.             //check left diagonals
  126.             accumulatedCols = 0;
  127.             for (var row = this.PositionOnBoard.Row - 1; row >= 0; row--)
  128.             {
  129.                 accumulatedCols++;
  130.                 if (this.PositionOnBoard.Col + accumulatedCols < Board.BoardSize)
  131.                     this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col + accumulatedCols));
  132.  
  133.                 if (this.PositionOnBoard.Col - accumulatedCols >= 0)
  134.                     this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col - accumulatedCols));
  135.             }
  136.         }
  137.     }
  138.  
  139.     internal class Board
  140.     {
  141.         public const int BoardSize = 8;
  142.  
  143.         public Board(List<Queen> queens)
  144.         {
  145.             this.Queens = queens;
  146.         }
  147.  
  148.         public List<Queen> Queens { get; }
  149.  
  150.         public void PrintBoard()
  151.         {
  152.             StringBuilder sb = new StringBuilder();
  153.             for (var row = 0; row < BoardSize; row++)
  154.             {
  155.                 for (var col = 0; col < BoardSize; col++)
  156.                 {
  157.                     Point currentPoint = new Point(row, col);
  158.  
  159.                     sb.Append(this.Queens.Exists(q => q.PositionOnBoard.Equals(currentPoint)) ? "* " : "- ");
  160.                 }
  161.  
  162.                 sb.AppendLine();
  163.             }
  164.  
  165.             //using (StreamWriter writer = new StreamWriter("output.txt", true))
  166.             //{
  167.             //    writer.Write(sb.ToString());
  168.             //    writer.WriteLine();
  169.             //}
  170.  
  171.             Console.WriteLine(sb.ToString());
  172.  
  173.         }
  174.  
  175.         public bool IsPointAttacked(Point point)
  176.         {
  177.             return this.Queens.Exists(q => q.IsAttackingPoint(point));
  178.         }
  179.     }
  180.  
  181.     internal class Point
  182.     {
  183.         public Point(int row, int col)
  184.         {
  185.             this.Row = row;
  186.             this.Col = col;
  187.         }
  188.  
  189.         public int Row { get; set; }
  190.  
  191.         public int Col { get; set; }
  192.  
  193.         public override bool Equals(object obj)
  194.         {
  195.             Point otherPoint = (Point)obj;
  196.  
  197.             return otherPoint.Row == this.Row && otherPoint.Col == this.Col;
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement