8皇后问题递归求解
代码说话 ;)
using System;
using System.Collections.Generic;
using System.Text;
namespace N皇后问题
{
class Recursion
{
static int queen_count ;
static int[] rows;
static int[] cross_left;
static int[] cross_right;
static int[] result;
static void Main(string[] args)
{
//使用递归的方式求解N皇后问题
while (true)
{
while (!get_input())
{
get_input();
}
rows = new int[queen_count];
cross_left = new int[2 * queen_count - 1];
cross_right = new int[2 * queen_count - 1];
result = new int[queen_count];
DateTime start = DateTime.Now;
put_queen(0);
TimeSpan used_time = (TimeSpan)(DateTime.Now - start);
Console.WriteLine();
Console.WriteLine(queen_count.ToString() + " 皇后的解为 :");
for (int queen = 0; queen < result.Length; queen++)
{
Console.Write(result[queen].ToString());//结果产生
Console.Write(" ");
}
Console.WriteLine();
Console.WriteLine("消耗时间: " + used_time.TotalSeconds.ToString() + " 秒");
Console.ReadLine();
}
}
static bool get_input()
{
Console.Clear();
Console.WriteLine("使用递归方法求解N皇后问题的一组解, 请输入皇后个数( 4< N <= 50 ) : ");
string str_n = Console.ReadLine();
if (string.IsNullOrEmpty(str_n))
{
error();
return false;
}
if (!int.TryParse(str_n, out queen_count))
{
error();
return false;
}
if (queen_count < 4 || queen_count > 50)
{
error();
return false;
}
return true;
}
static void error()
{
Console.WriteLine("请输入正确的数字!");
System.Threading.Thread.Sleep(1000);
}
static bool put_queen(int q_x)//列位置
{
if (q_x == (queen_count -1))
{
for (int k = 0; k < queen_count; k++)
{
if (canput((queen_count - 1), k))
{
result[(queen_count - 1)] = k;
return true;
}
}
return false;
}
for (int des_y = 0; des_y < queen_count; des_y++)//当前行位置
{
if (canput(q_x, des_y))
{
rows[des_y] = 1;
cross_left[des_y - q_x + queen_count -1] = 1;
cross_right[des_y + q_x] = 1;
result[q_x] = des_y;
if (put_queen(q_x + 1))
return true;
else
{
rows[des_y] = 0;
cross_left[des_y - q_x + queen_count -1] = 0;
cross_right[des_y + q_x] = 0;
}
}
else
continue;
}
return false;
}
static bool canput(int x, int y)
{
if (rows[y] == 1)
return false;
if (cross_left[y - x + queen_count -1] == 1)
return false;
if (cross_right[y + x] == 1)
return false;
return true;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace N皇后问题
{
class Recursion
{
static int queen_count ;
static int[] rows;
static int[] cross_left;
static int[] cross_right;
static int[] result;
static void Main(string[] args)
{
//使用递归的方式求解N皇后问题
while (true)
{
while (!get_input())
{
get_input();
}
rows = new int[queen_count];
cross_left = new int[2 * queen_count - 1];
cross_right = new int[2 * queen_count - 1];
result = new int[queen_count];
DateTime start = DateTime.Now;
put_queen(0);
TimeSpan used_time = (TimeSpan)(DateTime.Now - start);
Console.WriteLine();
Console.WriteLine(queen_count.ToString() + " 皇后的解为 :");
for (int queen = 0; queen < result.Length; queen++)
{
Console.Write(result[queen].ToString());//结果产生
Console.Write(" ");
}
Console.WriteLine();
Console.WriteLine("消耗时间: " + used_time.TotalSeconds.ToString() + " 秒");
Console.ReadLine();
}
}
static bool get_input()
{
Console.Clear();
Console.WriteLine("使用递归方法求解N皇后问题的一组解, 请输入皇后个数( 4< N <= 50 ) : ");
string str_n = Console.ReadLine();
if (string.IsNullOrEmpty(str_n))
{
error();
return false;
}
if (!int.TryParse(str_n, out queen_count))
{
error();
return false;
}
if (queen_count < 4 || queen_count > 50)
{
error();
return false;
}
return true;
}
static void error()
{
Console.WriteLine("请输入正确的数字!");
System.Threading.Thread.Sleep(1000);
}
static bool put_queen(int q_x)//列位置
{
if (q_x == (queen_count -1))
{
for (int k = 0; k < queen_count; k++)
{
if (canput((queen_count - 1), k))
{
result[(queen_count - 1)] = k;
return true;
}
}
return false;
}
for (int des_y = 0; des_y < queen_count; des_y++)//当前行位置
{
if (canput(q_x, des_y))
{
rows[des_y] = 1;
cross_left[des_y - q_x + queen_count -1] = 1;
cross_right[des_y + q_x] = 1;
result[q_x] = des_y;
if (put_queen(q_x + 1))
return true;
else
{
rows[des_y] = 0;
cross_left[des_y - q_x + queen_count -1] = 0;
cross_right[des_y + q_x] = 0;
}
}
else
continue;
}
return false;
}
static bool canput(int x, int y)
{
if (rows[y] == 1)
return false;
if (cross_left[y - x + queen_count -1] == 1)
return false;
if (cross_right[y + x] == 1)
return false;
return true;
}
}
}