8queens独特的解决方案 [英] 8queens Unique Solutions
本文介绍了8queens独特的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这就是我所拥有的.它是8个皇后的控制台版本,可以找到完整的解决方案(92).我想知道如何找到独特的解决方案(没有旋转和对称性).我已经尝试了一切,并想知道是否可以帮上点忙.
This is what I have. It''s a console version of the 8 queens and it can find the total solution (92). I am wondering how I am able to find the unique solutions(without rotations and symmetrys). I''ve tried everything and was wondering can help out just a teeny bit.
using System;
namespace EightQueen
{
public enum SIZE
{
size = 8
}
public class EightQueen
{
public static int count = 0;
public static bool isValidMove(int[,] boardArr, int row, int col)
{
for (int i = 0; i < SIZE.size.GetHashCode(); i++)
if (boardArr[row,i] == 1)
return false;
for (int i = 0; i < SIZE.size.GetHashCode(); i++)
if (boardArr[i,col] == 1)
return false;
int tempCol = col + 1, tempRow = row ;
for (int i = row + 1; i < SIZE.size.GetHashCode(); i++)
{
if (tempCol < SIZE.size.GetHashCode())
{
if (boardArr[i,tempCol] == 1)
return false;
else
tempCol++;
}
else
break;
}
tempCol = col - 1;
for (int i = row - 1; i >= 0; i--)
{
if (tempCol >= 0)
{
if (boardArr[i,tempCol] == 1)
return false;
else
tempCol --;
}
else
break;
}
tempCol = col - 1;
for (int i = row + 1; i < SIZE.size.GetHashCode(); i++)
{
if (tempCol >= 0)
{
if (boardArr[i, tempCol] == 1)
return false;
else
tempCol--;
}
else
break;
}
tempCol = col + 1;
for (int i = row - 1; i >= 0; i--)
{
if (tempCol < SIZE.size.GetHashCode())
{
if (boardArr[i, tempCol] == 1)
return false;
else
tempCol++;
}
else
break;
}
return true;
}
public static void ShouldPrintBoard(int[,] boardArr)
{
int count = 0;
for (int i = 0; i < SIZE.size.GetHashCode(); i++)
for(int j = 0; j < SIZE.size.GetHashCode() ;j++)
if (boardArr[i,j] == 1)
count++;
if (count >= 8)
PrintBoard(boardArr);
}
public static void PrintBoard(int[,] boardArray)
{
for (int i = 0; i < SIZE.size.GetHashCode(); i++)
{
for (int j = 0; j < SIZE.size.GetHashCode(); j++)
Console.Write("{0} ", boardArray[i, j]);
Console.WriteLine();
}
count++;
Console.WriteLine();
//Console.ReadKey();
}
public static void PermuteBoard(int[,] boardArr, int row, int col)
{
for (int i = row; i < SIZE.size.GetHashCode(); i++)
{
for (int j = 0; j < SIZE.size.GetHashCode(); j++)
{
if (isValidMove(boardArr, i, j))
{
boardArr[i, j] = 1;
ShouldPrintBoard(boardArr);
PermuteBoard(boardArr, i + 1, j + 1);
boardArr[i, j] = 0;
}
}
}
}
static void Main(string[] args)
{
int [,]boardArr = new int[SIZE.size.GetHashCode(),SIZE.size.GetHashCode()];
for(int i = 0 ; i < SIZE.size.GetHashCode() ; i++)
for(int j = 0 ; j < SIZE.size.GetHashCode() ; j++)
boardArr[i,j] = 0;
PermuteBoard(boardArr, 0, 0);
Console.WriteLine("Total Solutions {0}", count);
Console.ReadKey();
}
}
}
推荐答案
转到此处 [ ^ ]
以下是解决方案:
八个女王问题解决方案!
八位皇后拼图及其使用C#.Net的解决方案
Following is the solution:
Eight Queen Problem Solution!
Eight queens puzzle and its solution using C#.Net
这篇关于8queens独特的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文