Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace a06QueensPuzzle
- {
- class Program
- {
- private static int solutions = 0;
- static void Main(string[] args)
- {
- SolveQueensProblem(new List<Queen>(), 0);
- // Console.WriteLine(solutions);
- }
- private static void SolveQueensProblem(List<Queen> queens, int startingRow)
- {
- Board board = new Board(queens);
- System.GC.Collect(); //haha fuck you softuni I have become smarter
- if (queens.Count >= 8)
- {
- solutions++;
- board.PrintBoard();
- return;
- }
- if (startingRow >= 8)
- return;
- // if (solutions == 92) //faster way for exiting the recursion now that I know the number of solutions
- // {
- // throw new Exception();
- // }
- for (int col = 0; col < 8; col++)
- {
- Point currentPoint = new Point(startingRow, col);
- if (!board.IsPointAttacked(currentPoint))
- {
- List<Queen> newQueens = new List<Queen>(board.Queens) { new Queen(currentPoint) };
- SolveQueensProblem(newQueens, startingRow + 1);
- }
- }
- }
- }
- internal class Queen
- {
- private Point positionOnBoard;
- private List<Point> attackingPoints;
- public Queen(Point positionOnBoard)
- {
- this.PositionOnBoard = positionOnBoard;
- }
- public Point PositionOnBoard
- {
- get => positionOnBoard;
- set
- {
- this.positionOnBoard = value;
- this.InitAttackingPositions();
- }
- }
- public bool IsAttackingPoint(Point point)
- {
- return this.attackingPoints.Exists(p => p.Row == point.Row && p.Col == point.Col);
- }
- private void InitAttackingPositions()
- {
- this.attackingPoints = new List<Point>();
- this.AddRowAttackingPoints();
- this.AddColAttackingPoints();
- this.AddDiagonalAttackingPoints();
- this.attackingPoints.Add(this.PositionOnBoard);
- }
- private void AddRowAttackingPoints()
- {
- for (var row = this.PositionOnBoard.Row + 1; row < Board.BoardSize; row++)
- this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col));
- for (var row = this.PositionOnBoard.Row - 1; row >= 0; row--)
- this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col));
- }
- private void AddColAttackingPoints()
- {
- for (var col = this.PositionOnBoard.Col + 1; col < Board.BoardSize; col++)
- this.attackingPoints.Add(new Point(this.PositionOnBoard.Row, col));
- for (var col = this.PositionOnBoard.Col - 1; col >= 0; col--)
- this.attackingPoints.Add(new Point(this.PositionOnBoard.Row, col));
- }
- private void AddDiagonalAttackingPoints()
- {
- //check right diagonals
- var accumulatedCols = 0;
- for (var row = this.PositionOnBoard.Row + 1; row < Board.BoardSize; row++)
- {
- accumulatedCols++;
- if (this.PositionOnBoard.Col + accumulatedCols < Board.BoardSize)
- this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col + accumulatedCols));
- if (this.PositionOnBoard.Col - accumulatedCols >= 0)
- this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col - accumulatedCols));
- }
- //check left diagonals
- accumulatedCols = 0;
- for (var row = this.PositionOnBoard.Row - 1; row >= 0; row--)
- {
- accumulatedCols++;
- if (this.PositionOnBoard.Col + accumulatedCols < Board.BoardSize)
- this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col + accumulatedCols));
- if (this.PositionOnBoard.Col - accumulatedCols >= 0)
- this.attackingPoints.Add(new Point(row, this.PositionOnBoard.Col - accumulatedCols));
- }
- }
- }
- internal class Board
- {
- public const int BoardSize = 8;
- public Board(List<Queen> queens)
- {
- this.Queens = queens;
- }
- public List<Queen> Queens { get; }
- public void PrintBoard()
- {
- StringBuilder sb = new StringBuilder();
- for (var row = 0; row < BoardSize; row++)
- {
- for (var col = 0; col < BoardSize; col++)
- {
- Point currentPoint = new Point(row, col);
- sb.Append(this.Queens.Exists(q => q.PositionOnBoard.Equals(currentPoint)) ? "* " : "- ");
- }
- sb.AppendLine();
- }
- //using (StreamWriter writer = new StreamWriter("output.txt", true))
- //{
- // writer.Write(sb.ToString());
- // writer.WriteLine();
- //}
- Console.WriteLine(sb.ToString());
- }
- public bool IsPointAttacked(Point point)
- {
- return this.Queens.Exists(q => q.IsAttackingPoint(point));
- }
- }
- internal class Point
- {
- public Point(int row, int col)
- {
- this.Row = row;
- this.Col = col;
- }
- public int Row { get; set; }
- public int Col { get; set; }
- public override bool Equals(object obj)
- {
- Point otherPoint = (Point)obj;
- return otherPoint.Row == this.Row && otherPoint.Col == this.Col;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement