using System;
namespace sunjoy
{
public class Queen
{
public static int Main()
{
int board_size = 0,x=0,y=0;//棋盤大小,當前行,當前列
uint solution_count = 0; //皇後擺放方案的個數
int[] rows, cols, slot1, slot2, x2y;//行占用情況,列占用情況,“/”狀斜線占用情況,“\”狀斜線占用情況,皇後坐標
DateTime t_start, t_end;
Console.WriteLine("請輸入棋盤的大小");
try
{
board_size = Convert.ToInt32(Console.ReadLine());
if (board_size <= 0)
{
Console.WriteLine("非法數據");
return -1;
}
}
catch (Exception e) { Console.WriteLine("發生異常" + e.Message); };
rows = new int[board_size];
cols = new int[board_size];
slot1 = new int[board_size * 2 - 1];
slot2 = new int[board_size * 2 - 1];
x2y = new int[board_size];
for (int i = 0; i < board_size; i++)
x2y[i] = -1; //坐標初始化都為-1
t_start = DateTime.Now;
while (true)
{
for (y = x2y[x]+1; y < board_size; y++)
if (rows[x] == 0 && cols[y] == 0 && slot1[x + y] == 0 && slot2[x - y + board_size - 1] == 0)
break;
if (y < board_size)
{
//第X行的棋子落下
rows[x] = 1; cols[y] = 1; slot1[x + y] = 1; slot2[x - y + board_size - 1] = 1; x2y[x] = y;
}
else
{
//回溯,