Я сделал доску для раскрашенной восьми королевской проблемы, которая следовала приведенным ниже правилам. > Каждый столбец должен иметь ровно одну королеву < /li>
Queens не может касаться друг друга, даже диагонали < /li>
Каждая цветовая область должна иметь ровную ровную королеву < /li>
< /ol>
Пример: < /strong> < /p>
У меня есть код для размещения Queens на нерешенную плату. Но я изо всех сил пытаюсь найти все возможные раскрашенные доски. Но как я могу найти все возможные раскраски для решения? < /P>
public class Cell : ICloneable
{
public object Clone()
{
return new Cell(this.Status, this.Color);
}
public Cell(CellStatus status, int color)
{
this.Status = status;
this.Color = color;
}
public Cell()
{
}
public CellStatus Status { get; set; }
public int Color { get; set; }
}
public enum CellStatus
{
Empty,
Queen, //Q
NotAvailable //X
}
public static void SetAllColors(Cell[,] cells)
{
int color = 0;
//set initial different colors every queen cell
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (cells[i, j].Status == CellStatus.Queen)
{
color += 1;
cells[i, j].Color = color;
}
}
}
//fill remaining cells following the rule 4
while (EmptyCellExists(cells))
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (cells[i, j].Color != -1)
{
SetColorToNeighbourCell(cells, i, j, cells[i, j].Color);
}
}
}
}
public static void SetColorToNeighbourCell(Cell[,] cells, int a, int b, int color)
{
//find vertical and horizontal neighbour cells
int x1 = a - 1;
int x2 = a + 1;
int y1 = b - 1;
int y2 = b + 1;
var list = new List();
//check the neighbour cells if it is on the board and if it is available to set color
if(InBoard(x1,b) && cells[x1,b].Status != CellStatus.Queen && cells[x1,b].Color == -1)
list.Add(new Point(x1,b));
if(InBoard(x2,b) && cells[x2,b].Status != CellStatus.Queen && cells[x2,b].Color == -1)
list.Add(new Point(x2,b));
if(InBoard(a,y1) && cells[a,y1].Status != CellStatus.Queen && cells[a,y1].Color == -1)
list.Add(new Point(a,y1));
if(InBoard(a,y2) && cells[a,y2].Status != CellStatus.Queen && cells[a, y2].Color == -1)
list.Add(new Point(a,y2));
//if there is no suitable neighbour
if(list.Count < 1)
return;
//randomly select a neighbour
int i = rnd.Next(0, list.Count);
//randomly choose if we place a color to the selected cell or not, %50
int j = rnd.Next(0, 2);
if(j == 0)
{
int x = list.X;
int y = list.Y;
cells[x,y].Color = color;
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... en-problem
Как найти все возможные раскраски в задаче о восьми ферзях? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение