Advertisement
svetlozar_kirkov

Chess Queens

Sep 18th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ChessQueens
  6. {
  7.     class ChessQueens
  8.     {
  9.         static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             int distance = int.Parse(Console.ReadLine());
  13.             string[,] board = new string[n, n];
  14.             char initial = 'a';
  15.             List<string> collected = new List<string>();
  16.            
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 for (int j = 0; j < n; j++)
  20.                 {
  21.                     board[i, j] = Convert.ToString(initial) + Convert.ToString(j + 1);
  22.                    
  23.                 }
  24.                 initial++;
  25.             }
  26.  
  27.             if (distance >= n - 1)
  28.             {
  29.                 Console.WriteLine("No valid positions");
  30.                 return;
  31.             }
  32.             else
  33.             {
  34.                 for (int i = 0; i < n; i++)
  35.                 {
  36.                     for (int j = 0; j+distance+1 < n; j++)
  37.                     {
  38.                         string[] temp = new string[2] { board[i, j], board[i, j + distance + 1] };
  39.                         string one = temp[0] + " - " + temp[1];
  40.                         string two = temp[1] + " - " + temp[0];
  41.                         collected.Add(one);
  42.                         collected.Add(two);
  43.                     }
  44.                 }
  45.  
  46.                 for (int i = 0; i < n; i++)
  47.                 {
  48.                     for (int j = 0; j + distance + 1 < n; j++)
  49.                     {
  50.                         string[] temp = new string[2] { board[j, i], board[j + distance + 1, i]};
  51.                         string one = temp[0] + " - " + temp[1];
  52.                         string two = temp[1] + " - " + temp[0];
  53.                         collected.Add(one);
  54.                         collected.Add(two);
  55.                     }
  56.                 }
  57.                 for (int i = 0; i < n; i++)
  58.                 {
  59.                     for (int j = 0; j + distance + 1 < n && i+distance+1 < n; j++)
  60.                     {
  61.                         string[] temp = new string[2] { board[i, j], board[i + distance + 1, j + distance + 1] };
  62.                         string one = temp[0] + " - " + temp[1];
  63.                         string two = temp[1] + " - " + temp[0];
  64.                         collected.Add(one);
  65.                         collected.Add(two);
  66.                     }
  67.                 }
  68.                 for (int i = 0; i < n; i++)
  69.                 {
  70.                     for (int j = n - 1; j - distance - 1 >= 0 && i+distance+1 < n; j--)
  71.                     {
  72.                         string[] temp = new string[2] { board[i, j], board[i+distance+1, j-distance-1] };
  73.                         string one = temp[0] + " - " + temp[1];
  74.                         string two = temp[1] + " - " + temp[0];
  75.                         collected.Add(one);
  76.                         collected.Add(two);
  77.                     }
  78.                 }
  79.                
  80.             }
  81.  
  82.             List<string> noDupes = collected.Distinct().ToList();
  83.  
  84.             noDupes.Sort();
  85.  
  86.             foreach (var item in noDupes)
  87.             {
  88.                 Console.WriteLine(item);
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement