using System;
using System.Collections.Generic;
using System.Text;
namespace Queen
{
/*
* 八皇后问题
* @Author: Red_angelX
*/
class Program
{
const int NCOUNT = 8;
static int[] QueenMap = new int[NCOUNT];
static int iCount = 1;
static void Main(string[] args)
{
int current = System.Environment.TickCount;
PlayQueen(0);
Console.WriteLine("Eclped {0} ms", Environment.TickCount - current);
Console.Read();
}
/*
* 核心函数,放置第N枚皇后(递归)
*/
static void PlayQueen(int n)
{
if (n == NCOUNT)
{
PrintResult();
return;
}
for (int i = 1; i <= NCOUNT; i++)
{
QueenMap[n] = i;
if (IsValid(n))
PlayQueen(n+1);
}
}
/*
* 输出结果
*/
static void PrintResult()
{
Console.Write("No.{0} ", iCount++);
for (int i = 0; i < NCOUNT; i++)
Console.Write("{0} ", QueenMap[i]);
Console.Write(" ");
}
/*
* 检查第n个皇后放上去之后是否合法
*/
static bool IsValid(int n)
{
for (int i = 0; i < n; i++)
{
if (QueenMap[i] == QueenMap[n])
return false;
if (Math.Abs(QueenMap[i] - QueenMap[n]) == n - i)
return false;
}
return true;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace Queen
{
/*
* 八皇后问题
* @Author: Red_angelX
*/
class Program
{
const int NCOUNT = 8;
static int[] QueenMap = new int[NCOUNT];
static int iCount = 1;
static void Main(string[] args)
{
int current = System.Environment.TickCount;
PlayQueen(0);
Console.WriteLine("Eclped {0} ms", Environment.TickCount - current);
Console.Read();
}
/*
* 核心函数,放置第N枚皇后(递归)
*/
static void PlayQueen(int n)
{
if (n == NCOUNT)
{
PrintResult();
return;
}
for (int i = 1; i <= NCOUNT; i++)
{
QueenMap[n] = i;
if (IsValid(n))
PlayQueen(n+1);
}
}
/*
* 输出结果
*/
static void PrintResult()
{
Console.Write("No.{0} ", iCount++);
for (int i = 0; i < NCOUNT; i++)
Console.Write("{0} ", QueenMap[i]);
Console.Write(" ");
}
/*
* 检查第n个皇后放上去之后是否合法
*/
static bool IsValid(int n)
{
for (int i = 0; i < n; i++)
{
if (QueenMap[i] == QueenMap[n])
return false;
if (Math.Abs(QueenMap[i] - QueenMap[n]) == n - i)
return false;
}
return true;
}
}
}
执行结果(Release版本):
No.1 1 5 8 6 3 7 2 4
No.2 1 6 8 3 7 4 2 5
No.3 1 7 4 6 8 2 5 3
No.4 1 7 5 8 2 4 6 3
No.5 2 4 6 8 3 1 7 5
No.6 2 5 7 1 3 8 6 4
No.7 2 5 7 4 1 8 6 3
No.8 2 6 1 7 4 8 3 5
No.9 2 6 8 3 1 4 7 5
No.10 2 7 3 6 8 5 1 4
No.11 2 7 5 8 1 4 6 3
No.12 2 8 6 1 3 5 7 4
No.13 3 1 7 5 8 2 4 6
No.14 3 5 2 8 1 7 4 6
No.15 3 5 2 8 6 4 7 1
No.16 3 5 7 1 4 2 8 6
No.17 3 5 8 4 1 7 2 6
No.18 3 6 2 5 8 1 7 4
No.19 3 6 2 7 1 4 8 5
No.20 3 6 2 7 5 1 8 4
No.21 3 6 4 1 8 5 7 2
No.22 3 6 4 2 8 5 7 1
No.23 3 6 8 1 4 7 5 2
No.24 3 6 8 1 5 7 2 4
No.25 3 6 8 2 4 1 7 5
No.26 3 7 2 8 5 1 4 6
No.27 3 7 2 8 6 4 1 5
No.28 3 8 4 7 1 6 2 5
No.29 4 1 5 8 2 7 3 6
No.30 4 1 5 8 6 3 7 2
No.31 4 2 5 8 6 1 3 7
No.32 4 2 7 3 6 8 1 5
No.33 4 2 7 3 6 8 5 1
No.34 4 2 7 5 1 8 6 3
No.35 4 2 8 5 7 1 3 6
No.36 4 2 8 6 1 3 5 7
No.37 4 6 1 5 2 8 3 7
No.38 4 6 8 2 7 1 3 5
No.39 4 6 8 3 1 7 5 2
No.40 4 7 1 8 5 2 6 3
No.41 4 7 3 8 2 5 1 6
No.42 4 7 5 2 6 1 3 8
No.43 4 7 5 3 1 6 8 2
No.44 4 8 1 3 6 2 7 5
No.45 4 8 1 5 7 2 6 3
No.46 4 8 5 3 1 7 2 6
No.47 5 1 4 6 8 2 7 3
No.48 5 1 8 4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 4 8 1 3 6
No.60 5 7 2 6 3 1 4 8
No.61 5 7 2 6 3 1 8 4
No.62 5 7 4 1 3 8 6 2
No.63 5 8 4 1 3 6 2 7
No.64 5 8 4 1 7 2 6 3
No.65 6 1 5 2 8 3 7 4
No.66 6 2 7 1 3 5 8 4
No.67 6 2 7 1 4 8 5 3
No.68 6 3 1 7 5 8 2 4
No.69 6 3 1 8 4 2 7 5
No.70 6 3 1 8 5 2 4 7
No.71 6 3 5 7 1 4 2 8
No.72 6 3 5 8 1 4 2 7
No.73 6 3 7 2 4 8 1 5
No.74 6 3 7 2 8 5 1 4
No.75 6 3 7 4 1 8 2 5
No.76 6 4 1 5 8 2 7 3
No.77 6 4 2 8 5 7 1 3
No.78 6 4 7 1 3 5 2 8
No.79 6 4 7 1 8 2 5 3
No.80 6 8 2 4 1 7 5 3
No.81 7 1 3 8 6 4 2 5
No.82 7 2 4 1 8 5 3 6
No.83 7 2 6 3 1 4 8 5
No.84 7 3 1 6 8 5 2 4
No.85 7 3 8 2 5 1 6 4
No.86 7 4 2 5 8 1 3 6
No.87 7 4 2 8 6 1 3 5
No.88 7 5 3 1 6 8 2 4
No.89 8 2 4 1 7 5 3 6
No.90 8 2 5 3 1 7 4 6
No.91 8 3 1 6 2 5 7 4
No.92 8 4 1 3 6 2 7 5
Eclped 94 ms