Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ConsoleApplication {
  6.     internal class Program {
  7.         private static readonly Stack<Point> OccupyHistory = new Stack<Point>();
  8.         private static long _fails = 0;
  9.  
  10.         public static void Main(string[] args) {
  11.             var chessTable = new ChessTable();
  12.             var combinationsCount = GetCombinationsCount(chessTable, 8);
  13.             Console.WriteLine("Combinations count: " + combinationsCount);
  14.             Console.WriteLine("Fails count: " + _fails);
  15.         }
  16.  
  17.         private static int GetCombinationsCount(ChessTable table, int figures) {
  18.             var count = 0;
  19.             for (var i = 0; i < ChessTable.Rows; i++) {
  20.                 for (var j = 0; j < ChessTable.Rows; j++) {
  21.                     count += StartPlacingAt(table, new Point(i, j), 1, figures);
  22.                     table.Clear();
  23.                 }
  24.             }
  25.  
  26.             return count;
  27.         }
  28.  
  29.         private static int StartPlacingAt(ChessTable table, Point point, int current, int max) {
  30.             if (current == max)
  31.                 return 1;
  32.  
  33.             FigureOccupyTablePoint(table, point);
  34.  
  35.             var count = 0;
  36.             for (var j = 0; j < ChessTable.Rows; j++) {
  37.                 var cell = table[current, j];
  38.                 if (cell.IsOccupied || cell.Attacked > 0)
  39.                     continue;
  40.  
  41.                 count += StartPlacingAt(table, new Point(current, j), current + 1, max);
  42.             }
  43.  
  44.             RollbackOccupyPoint(table);
  45.             _fails++;
  46.             return count;
  47.         }
  48.  
  49.         private static void FigureOccupyTablePoint(ChessTable table, Point point) {
  50.             OccupyHistory.Push(point);
  51.             table[point].IsOccupied = true;
  52.             SetAttacked(table, point, true);
  53.         }
  54.  
  55.         private static void RollbackOccupyPoint(ChessTable table) {
  56.             var point = OccupyHistory.Pop();
  57.             table[point].IsOccupied = false;
  58.             SetAttacked(table, point, false);
  59.         }
  60.  
  61.         private static void SetAttacked(ChessTable table, Point point, bool isAttacked) {
  62.             AttackCast(table, point, isAttacked, p => p + Point.Up);
  63.             AttackCast(table, point, isAttacked, p => p + Point.Down);
  64.             AttackCast(table, point, isAttacked, p => p + Point.Right);
  65.             AttackCast(table, point, isAttacked, p => p + Point.Left);
  66.             AttackCast(table, point, isAttacked, p => p + Point.Up + Point.Left);
  67.             AttackCast(table, point, isAttacked, p => p + Point.Down + Point.Left);
  68.             AttackCast(table, point, isAttacked, p => p + Point.Up + Point.Right);
  69.             AttackCast(table, point, isAttacked, p => p + Point.Down + Point.Right);
  70.         }
  71.  
  72.         private static void AttackCast(ChessTable table, Point start, bool isAttack, Func<Point, Point> move) {
  73.             while (true) {
  74.                 start = move(start);
  75.                 if (!table.PointInRange(start))
  76.                     return;
  77.                 table[start].Attacked += isAttack ? 1 : -1;
  78.             }
  79.         }
  80.  
  81.         public class ChessTable {
  82.             public const int Rows = 8;
  83.  
  84.             private readonly Cell[,] _table = new Cell[Rows, Rows];
  85.  
  86.             public ChessTable() {
  87.                 Clear();
  88.             }
  89.  
  90.             public Cell this[int i, int j] => _table[i, j];
  91.             public Cell this[Point point] => _table[point.X, point.Y];
  92.  
  93.             public bool PointInRange(Point point)
  94.                 => point.X > -1 && point.X < Rows && point.Y > -1 && point.Y < Rows;
  95.  
  96.             public void Clear() {
  97.                 for (var i = 0; i < Rows; i++)
  98.                 for (var j = 0; j < Rows; j++)
  99.                     _table[i, j] = new Cell();
  100.             }
  101.  
  102.             public class Cell {
  103.                 public int Attacked;
  104.                 public bool IsOccupied;
  105.             }
  106.  
  107.             public override string ToString() {
  108.                 var builder = new StringBuilder();
  109.  
  110.                 for (var i = 0; i < ChessTable.Rows; i++) {
  111.                     for (var j = 0; j < ChessTable.Rows; j++) {
  112.                         var cell = _table[i, j];
  113.                         if (cell.IsOccupied)
  114.                             builder.Append("@");
  115.                         else if (cell.Attacked > 0)
  116.                             builder.Append("X");
  117.                         else
  118.                             builder.Append("O");
  119.                     }
  120.  
  121.                     builder.Append("\r");
  122.                 }
  123.  
  124.                 return builder.ToString();
  125.             }
  126.         }
  127.  
  128.  
  129.         public struct Point {
  130.             public static readonly Point Up = new Point(0, 1);
  131.             public static readonly Point Down = new Point(0, -1);
  132.             public static readonly Point Right = new Point(1, 0);
  133.             public static readonly Point Left = new Point(-1, 0);
  134.             public static readonly Point Zero = new Point(0, 0);
  135.  
  136.             public int X;
  137.             public int Y;
  138.  
  139.             public Point(int x, int y) {
  140.                 X = x;
  141.                 Y = y;
  142.             }
  143.  
  144.             public static Point operator +(Point a, Point b) {
  145.                 return new Point(a.X + b.X, a.Y + b.Y);
  146.             }
  147.  
  148.             public static Point operator -(Point a, Point b) {
  149.                 return new Point(a.X - b.X, a.Y - b.Y);
  150.             }
  151.  
  152.             public override string ToString() => "X: " + X + " Y: " + Y;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement