Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
koko
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { DisplayPositions(4); } private static void DisplayPositions(int boardSize) { if (boardSize <= 0) return; var positions = new List<Position>(); if (GetPositions(positions, boardSize, 0, 0)) { Console.WriteLine(string.Join(",", positions)); } else { Console.WriteLine("Nothing to display"); } } private static bool GetPositions(List<Position> positions, int boardSize, int row, int column) { if (positions.Count == boardSize) { return true; } else { var rowIndex = row; var columnIndex = column; while (rowIndex < boardSize) { if (ValidPosition(positions, rowIndex, columnIndex)) { positions.Add(new Position(rowIndex, columnIndex)); if (GetPositions(positions, boardSize, rowIndex + 1, 0)) { return true; } positions.RemoveAt(positions.Count - 1); } columnIndex++; if (columnIndex == boardSize) { columnIndex = 0; rowIndex++; } } return false; } } private static bool ValidPosition(List<Position> positions, int rowIndex, int columnIndex) { foreach (var position in positions) { if (position.Row == rowIndex || position.Column == columnIndex) return false; if (Math.Abs(position.Row - rowIndex) == Math.Abs(position.Column - columnIndex)) { return false; } } return true; } } internal class Position { public Position(int row, int column) { Row = row; Column = column; } public int Row { get; set; } public int Column { get; set; } public override string ToString() { return "R:"+ Row+ "-C:" + Column; } } }
run
|
edit
|
history
|
help
0
Towers of Hanoi
Read XML nodes into Data Dictionary
Triangulo de pascal
How to use Lambda expression wiht generic delegate
Irfan_Problem
merge sort
BreakNum
X o X
examen res
lilnq 1