Advertisement
mustafov

Chess Queens

Sep 3rd, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. //Chess Queens
  2. //We are given a chess board of size N * N. The only figures we have on the    
  3. //chess board are two queens. The queen in chess can move in horizontal, vertical and diagonal directions. We are also given a //number D which represents the distance between the two queens. The distance is measured by D squares away. All positions on the //chessboard are represented with numbers and letters (a1, a2… a8, b1-b8, c1-c8, …, h1-h8). Example: if N=16, the numbers on the //board will be represented with integers (1-16) and letters (a-o). Your task is to find all couples of queens where the queens //stay either on the same vertical, horizontal or diagonal, at distance D. See the diagram aside to understand your task better. //The green queens meet the condition of 2 blocks away but the red queens aren’t.
  4.  
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ConsoleApplication2
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             int n = int.Parse(Console.ReadLine());
  19.             int d = int.Parse(Console.ReadLine());
  20.  
  21.             int lastNum = '`' + n;
  22.             char lastLetter = (char)lastNum;
  23.  
  24.             int count = 0;
  25.            
  26.             for (char firstLetter = 'a'; firstLetter <= lastLetter; firstLetter++)
  27.             {
  28.                 for (int firstNum = 1; firstNum <= n; firstNum++)
  29.                 {
  30.                     for (int secondLetter = 'a'; secondLetter <= lastLetter; secondLetter++ )
  31.                     {
  32.                         for (int secondNum = 1; secondNum <= n; secondNum++)
  33.                         {
  34.                             if (firstLetter == secondLetter && secondNum - firstNum == d + 1)
  35.                             {
  36.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  37.                                 count++;
  38.                             }
  39.                             if (firstNum == secondNum && secondLetter - firstLetter == d + 1)
  40.                             {
  41.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  42.                                 count++;
  43.                             }
  44.                             if (firstLetter == secondLetter && firstNum - secondNum == d + 1)
  45.                             {
  46.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  47.                                 count++;
  48.                             }
  49.                             if (firstNum == secondNum && firstLetter - secondLetter == d + 1)
  50.                             {
  51.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  52.                                 count++;
  53.                             }
  54.                             if (firstLetter - secondLetter == d + 1 && firstNum - secondNum == d + 1)
  55.                             {
  56.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  57.                                 count++;
  58.                             }
  59.                             if (secondLetter - firstLetter == d + 1 && secondNum - firstNum == d + 1)
  60.                             {
  61.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  62.                                 count++;
  63.                             }
  64.                             if (firstLetter - secondLetter == d + 1 && secondNum - firstNum == d + 1)
  65.                             {
  66.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  67.                                 count++;
  68.                             }
  69.                             if (secondLetter - firstLetter == d + 1 && firstNum - secondNum == d + 1)
  70.                             {
  71.                                 Console.WriteLine("{0}{1} - {2}{3}", (char)firstLetter, firstNum, (char)secondLetter, secondNum);
  72.                                 count++;
  73.                             }
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.             if (count == 0)
  79.             {
  80.                 Console.WriteLine("No valid positions");
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement