Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ChessQueens
- {
- class ChessQueens
- {
- static void Main()
- {
- int size = int.Parse(Console.ReadLine());
- int[,] matrix = new int[size, size];
- int step = int.Parse(Console.ReadLine());
- int resultCounter = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- //check horizontal
- //down
- try
- {
- int extension = row + 1 + step;
- if (matrix[row, col] == matrix[extension, col])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(extension) + TranslateToChessCol(col));
- resultCounter++;
- }
- }
- catch (Exception) { }
- //up
- try
- {
- int extension = row - 1 - step;
- if (matrix[row, col] == matrix[extension, col])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(extension) + TranslateToChessCol(col));
- resultCounter++;
- }
- }
- catch (Exception) { }
- //left
- try
- {
- int extension = col - 1 - step;
- if (matrix[row, col] == matrix[row, extension])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(row) + TranslateToChessCol(extension));
- resultCounter++;
- }
- }
- catch (Exception) { }
- //right
- try
- {
- int extension = col + 1 + step;
- if (matrix[row, col] == matrix[row, extension])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(row) + TranslateToChessCol(extension));
- resultCounter++;
- }
- }
- catch (Exception) { }
- //check diagonals
- //up-left
- try
- {
- int extensionRow = row - 1 - step;
- int extensionCol = col - 1 - step;
- if (matrix[row, col] == matrix[extensionRow, extensionCol])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
- resultCounter++;
- }
- }
- catch (Exception) { }
- //up-right
- try
- {
- int extensionRow = row - 1 - step;
- int extensionCol = col + 1 + step;
- if (matrix[row, col] == matrix[extensionRow, extensionCol])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
- resultCounter++;
- }
- }
- catch (Exception) { }
- //down-left
- try
- {
- int extensionRow = row + 1 + step;
- int extensionCol = col - 1 - step;
- if (matrix[row, col] == matrix[extensionRow, extensionCol])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
- resultCounter++;
- }
- }
- catch (Exception) { }
- //down-right
- try
- {
- int extensionRow = row + 1 + step;
- int extensionCol = col + 1 + step;
- if (matrix[row, col] == matrix[extensionRow, extensionCol])
- {
- Console.WriteLine("" + TranslateToChessRow(row) + TranslateToChessCol(col)
- + " - " + TranslateToChessRow(extensionRow) + TranslateToChessCol(extensionCol));
- resultCounter++;
- }
- }
- catch (Exception) { }
- }
- }
- if (resultCounter == 0)
- {
- Console.WriteLine("No valid positions");
- }
- }
- public static char TranslateToChessRow(int number)
- {
- return (char)(number + 97);
- }
- public static int TranslateToChessCol(int number)
- {
- return number + 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement