一、问题描述
八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行、纵行或斜线上。
二、整体设计思路
1.用web控件Table实现棋盘的布局
Table控件在后台用代码生成,多个单元格TableCell构成一行TableRow,而多个行构成一个表格Table。
采用的语句有
(1)TableRow.Cells.Add(TableCell) 单元格插入到行
(2)Table.Rows.Add(TableRow) 行插入到表格
(3)Holder.Controls.Add(Tabel) 表格插入到容器
行列之和为奇数时候单元格背景为黑色,对应Css设计td.black,否则为白色,对应Css设计td.black;
2、皇后放置,将单元格的背景图片换成80*80的皇后的图片,对应Css设计td.queen
3、Asp.net页面设计,一个HTML标题控件<h2>标题</h2>,左右两个div层,#div_left ,#div_right ,左边放置计算按钮、显示排列结果的ListBox和排列总个数的lable控件,右边是一个PlaceHolder控件,用来放置生成的Table控件
4.八皇后问题的递归算法求解,就得的结果放置在Lisitem中,再将Listitem放入ListBox中
5.将最终的排列的总个数在label控件中显示
三、代码
1.前台界面设计
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="queen.aspx.cs" Inherits="queen" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
table {
border:1px solid black;
border-collapse:collapse;
}
td {
border:1px solid gray;
width:80px;
height:80px;
}
td.black {
background-color:black;
}
td.queen{
background-image:url(queen.bmp);
background-position:center;
background-repeat:no-repeat;
}
#div_left{
width:460px;
float:left;
}
#div_right
{
width:640px;
float:right;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>八皇后问题</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:1100px;margin:0px auto">
<h2 style="text-align:center;border-bottom:1px solid gray;padding-bottom:10px">C#语言实现八皇后问题求解</h2>
<div id="div_left">
<asp:Button ID="Button1" runat="server" Text="求解" OnClick="Button1_Click" />
<asp:ListBox ID="lbo_res" Width="460px" Height="400px" runat="server" AutoPostBack="True" OnSelectedIndexChanged="lbo_res_SelectedIndexChanged"></asp:ListBox>
<asp:Label ID="lbl_CountRes" runat="server" Text="八皇后问题解的个数是" Width="460px"></asp:Label>
</div>
<div id="div_right">
<asp:PlaceHolder ID="holder_table" runat="server" ></asp:PlaceHolder>
</div>
</div>
</form>
</body>
</html>
2.后台代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class queen : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//x存放放置位置,cout存放放置方法的总个数
int[] x = new int[8];
int cout = 0;
public void Queen(int t)
{
if (t==8)
{
ListItem li=new ListItem();
cout++;//sum为所有的可行解
for (int i = 0; i < 8; i++)
{
li.Text += x[i].ToString();
}
lbo_res.Items.Add(li);
}
else
{
for (int i = 0; i < 8; i++)
{
x[t] = i;
if (IsSafe(t)) {
Queen(t + 1);
}
}
}
}
//检查皇后放置是否合法
public bool IsSafe(int r)
{
for (int i = 0; i < r; i++)//遍历该行之前
{
if (Math.Abs(x[r] - x[i]) == Math.Abs(r - i) || x[i] == x[r])
{
return false;
}
}
return true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Queen(0);
lbl_CountRes.Text += cout.ToString();
}
protected void lbo_res_SelectedIndexChanged(object sender, EventArgs e)
{
Table _tbl = new Table();
TableRow _row;
TableCell _cell;
string s = lbo_res.SelectedItem.Text;
char[] arrry = s.ToCharArray();
for (int r = 0; r < 8; r++)
{
_row = new TableRow();
for (int c = 0; c < 8; c++)
{
_cell = new TableCell();
if ((r + c) % 2 == 1)//行列之和为奇数时候单元格背景为黑色
{
_cell.CssClass = "black";
}
if (c.ToString() == arrry[r].ToString())
{
_cell.CssClass += " queen";
}
_row.Cells.Add(_cell);
}
_tbl.Rows.Add(_row);
}
holder_table.Controls.Add(_tbl);
}
}四、效果展示
本文详细介绍了如何使用C#语言和Visual Studio解决经典的八皇后问题。通过创建棋盘布局,利用web控件Table,并结合CSS进行样式设计。接着阐述了皇后放置逻辑,以及Asp.net页面结构,包括计算按钮、结果显示ListBox和计数Label。文章重点在于八皇后问题的递归算法实现,求解结果展示在ListBox中,并显示总的解决方案数量。

1297

被折叠的 条评论
为什么被折叠?



