Advertisement
Guest User

Untitled

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