这个是那时候想的算法,用C#写的程序,共享一下:)
运行效果抓图
不知道为什么上传文件不好使,那只有粘上来代码了,建立一个WinForm工程把下面代码粘进去。可以找一个自己喜欢的图片作为棋子与exe放在一起,重名为棋子.bmp
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace bahuanghou
... {
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(472, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 32);
this.label1.TabIndex = 0;
this.label1.Text = "0";
//
// button1
//
this.button1.Location = new System.Drawing.Point(504, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(32, 32);
this.button1.TabIndex = 1;
this.button1.Text = ">>";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(448, 72);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(32, 32);
this.button2.TabIndex = 2;
this.button2.Text = "<<";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(408, 160);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(552, 389);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new Form1());
}
private static int QMax=8;
private static Point [] places;
private static int [,]QiPan=new int[8,8];
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ComboBox comboBox1;
private static ArrayList total=new ArrayList();
private static Image image;
private void Form1_Load(object sender, System.EventArgs e)
...{
calculateQueen();
for(int i=0;i<total.Count;i++)
comboBox1.Items.Add(i.ToString());
image=Image.FromFile("棋子.bmp");
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
...{
Graphics g=e.Graphics;
drawQiPan(g,40,new Point(5,10));
// drawWangHou(g,new Point(5,10),40,new Point(2,5));
// initForCal();
Point [] points=(Point [])total[Int32.Parse(label1.Text)];
for(int i=0;i<QMax;i++)
...{
drawWangHou(g,new Point(5,10),40,points[i]);
}
}
private void drawQiPan(Graphics g,int everySize,Point startPos)
...{
Brush bb=Brushes.Black;
Brush bw=Brushes.White;
for(int j=0;j<8;j++)
...{
for(int i=0;i<4;i++)
...{
g.FillRectangle(bb,new Rectangle(
new Point(startPos.X+i*everySize*2,
startPos.Y+j*everySize),new Size(everySize,everySize)));
g.FillRectangle(bw,new Rectangle(
new Point(startPos.X+everySize+i*everySize*2,
startPos.Y+j*everySize),new Size(everySize,everySize)));
}
Brush t=bb;
bb=bw;
bw=t;
}
}
private void drawWangHou(Graphics g,Point startPos,int size,Point pos)
...{
int x=startPos.X+(pos.X*size);
int y=startPos.Y+(pos.Y*size);
// g.FillEllipse(Brushes.Red,new Rectangle(new Point(x,y),new Size(size,size)));
g.DrawImage(image,new Rectangle(new Point(x,y),new Size(size,size)));
}
public static void initForCal()
...{
places=new Point[QMax];
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
...{
QiPan[i,j]=1;//1 表示棋盘当前可以放棋子
//9 表示棋盘当前已经放子了
//-1 表示棋盘当前位置不可以放子
//-n 表示这是由第n行导致的不能放旗子
}
}
public static void calculateQueen()
...{
/**//*逐行扫描
当push成功时,stack.push(当前坐标)
当扫描到某行却一次也push不成功时
*/
int startX=0;//每次开始的x坐标
// int num=0;
int y=0;
Stack stack=new Stack();
initForCal();
while(true)
...{
bool bPush=Push(startX,y);
if(bPush)
...{
stack.Push(new Point(startX,y));
y++;
startX=0;
if(stack.Count==8)
...{
Stack copyStack=(Stack)stack.Clone();
for(int i=0;i<places.Length;i++)
...{
places[i]=(Point)copyStack.Pop();
}
// if(total.Contains(places))
// {//如果已经存在
// break;//跳出循环
// }
string sss=null;
foreach(Point ps in places)
...{
sss+=(ps.ToString()+" ");
}
// Console.WriteLine(sss);
total.Add(places);
places=new Point[8];
Point p=(Point)stack.Pop();
startX=p.X;
y=p.Y;
cancelEffect(startX,y,1);
QiPan[startX,y]=1;
startX++;
}
}
else if(startX>=8)
...{
if(stack.Count==0)
...{
break;
}
Point p=(Point)stack.Pop();
y=p.Y;
startX=p.X;
//
cancelEffect(startX,y,1);
QiPan[startX,y]=1;
startX++;
// coutQiPan();
}
else if(!bPush && startX<8)
...{
startX++;
}
// coutQiPan();
}
}
private static void coutQiPan()
...{/**/////this funcation is used to debug
Console.WriteLine("棋盘:");
for(int i=0;i<8;i++)
...{
for(int j=0;j<8;j++)
...{
Console.Write(QiPan[j,i].ToString(" 0;-#;")+" ");
}
Console.Write(" ");
}
}
private static void forcancel(int [,] qp,int x,int y,int row,int setValue)
...{
if(qp[x,y]==(0-row))
qp[x,y]=setValue;
}
public static void cancelEffect(int x,int y,int flag)//flag=1
...{
for(int i=0;i<8;i++)
...{/**////将横和竖标记为不可以放棋子了
// if(QiPan[x,i]==(0-y))
// QiPan[x,i]=flag;
forcancel(QiPan,x,i,y,flag);
forcancel(QiPan,i,y,y,flag);
//if(QiPan[y,i]=flag;
}
//下面将斜位置标记为不可以放棋子
int n=0;
while(x-n>=0 && y-n>=0) //将左斜上方
...{
forcancel(QiPan,x-n,y-n,y,flag);
//QiPan[x-n,y-n]=flag;
n++;
}
n=0;
while(x+n<8 && y+n<8) //右下
...{
forcancel(QiPan,x+n,y+n,y,flag);
//QiPan[x+n,y+n]=flag;
n++;
}
n=0;
int k=0;
while(x+n<8 && y-k>=0)//右上
...{
forcancel(QiPan,x+n,y-k,y,flag);
//QiPan[x+n,y-k]=flag;
n++;k++;
}
n=0;k=0;
while(x-n>=0 && y+k<8)//左下
...{
forcancel(QiPan,x-n,y+k,y,flag);
// QiPan[x-n,y+k]=flag;
n++;k++;
}
//
}
public static bool canPush(int x,int y)
...{
if(x<0 || x>=8 || y<0 || y>=8)return false;
return (QiPan[x,y]<=0 || QiPan[x,y]==9 )?false:true;
}
private static void fortag(int[,] qp,int x,int y,int flag)
...{
if(qp[x,y]==1)
qp[x,y]=flag;
}
public static void tag(int x,int y,int flag)
...{
flag=-y;
for(int i=0;i<8;i++)
...{/**////将横和竖标记为不可以放棋子了
// QiPan[x,i]=flag;
// QiPan[i,y]=flag;
fortag(QiPan,x,i,flag);
fortag(QiPan,i,y,flag);
}
//下面将斜位置标记为不可以放棋子
int n=0;
while(x-n>=0 && y-n>=0) //将左斜上方
...{
// QiPan[x-n,y-n]=flag;
fortag(QiPan,x-n,y-n,flag);
n++;
}
n=0;
while(x+n<8 && y+n<8) //右下
...{
// QiPan[x+n,y+n]=flag;
fortag(QiPan,x+n,y+n,flag);
n++;
}
n=0;
int k=0;
while(x+n<8 && y-k>=0)//右上
...{
// QiPan[x+n,y-k]=flag;
fortag(QiPan,x+n,y-k,flag);
n++;k++;
}
n=0;k=0;
while(x-n>=0 && y+k<8)//左下
...{
// QiPan[x-n,y+k]=flag;
fortag(QiPan,x-n,y+k,flag);
n++;k++;
}
//
}
public static bool Push(int x,int y)
...{
if(!canPush(x,y))
...{
// MessageBox.Show("shit");
return false;
}
tag(x,y,-1);//标记出棋盘上不能放棋子的位置
QiPan[x,y]=9;//将当前位置标记为已经放入棋子
return true;
}
private void button1_Click(object sender, System.EventArgs e)
...{
int current=Int32.Parse(label1.Text);
if(current>=total.Count-1)
return;
int now=current+1;
label1.Text=now.ToString();
this.Refresh();
}
private void button2_Click(object sender, System.EventArgs e)
...{
int current=Int32.Parse(label1.Text);
if(current<=0)
return;
int now=current-1;
label1.Text=now.ToString();
this.Refresh();
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
...{
int current=Int32.Parse(comboBox1.Text);
if(current>=total.Count || current<0)
return;
int now=current;
label1.Text=now.ToString();
this.Refresh();
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace bahuanghou
... {
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(472, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(40, 32);
this.label1.TabIndex = 0;
this.label1.Text = "0";
//
// button1
//
this.button1.Location = new System.Drawing.Point(504, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(32, 32);
this.button1.TabIndex = 1;
this.button1.Text = ">>";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(448, 72);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(32, 32);
this.button2.TabIndex = 2;
this.button2.Text = "<<";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(408, 160);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(552, 389);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new Form1());
}
private static int QMax=8;
private static Point [] places;
private static int [,]QiPan=new int[8,8];
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ComboBox comboBox1;
private static ArrayList total=new ArrayList();
private static Image image;
private void Form1_Load(object sender, System.EventArgs e)
...{
calculateQueen();
for(int i=0;i<total.Count;i++)
comboBox1.Items.Add(i.ToString());
image=Image.FromFile("棋子.bmp");
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
...{
Graphics g=e.Graphics;
drawQiPan(g,40,new Point(5,10));
// drawWangHou(g,new Point(5,10),40,new Point(2,5));
// initForCal();
Point [] points=(Point [])total[Int32.Parse(label1.Text)];
for(int i=0;i<QMax;i++)
...{
drawWangHou(g,new Point(5,10),40,points[i]);
}
}
private void drawQiPan(Graphics g,int everySize,Point startPos)
...{
Brush bb=Brushes.Black;
Brush bw=Brushes.White;
for(int j=0;j<8;j++)
...{
for(int i=0;i<4;i++)
...{
g.FillRectangle(bb,new Rectangle(
new Point(startPos.X+i*everySize*2,
startPos.Y+j*everySize),new Size(everySize,everySize)));
g.FillRectangle(bw,new Rectangle(
new Point(startPos.X+everySize+i*everySize*2,
startPos.Y+j*everySize),new Size(everySize,everySize)));
}
Brush t=bb;
bb=bw;
bw=t;
}
}
private void drawWangHou(Graphics g,Point startPos,int size,Point pos)
...{
int x=startPos.X+(pos.X*size);
int y=startPos.Y+(pos.Y*size);
// g.FillEllipse(Brushes.Red,new Rectangle(new Point(x,y),new Size(size,size)));
g.DrawImage(image,new Rectangle(new Point(x,y),new Size(size,size)));
}
public static void initForCal()
...{
places=new Point[QMax];
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
...{
QiPan[i,j]=1;//1 表示棋盘当前可以放棋子
//9 表示棋盘当前已经放子了
//-1 表示棋盘当前位置不可以放子
//-n 表示这是由第n行导致的不能放旗子
}
}
public static void calculateQueen()
...{
/**//*逐行扫描
当push成功时,stack.push(当前坐标)
当扫描到某行却一次也push不成功时
*/
int startX=0;//每次开始的x坐标
// int num=0;
int y=0;
Stack stack=new Stack();
initForCal();
while(true)
...{
bool bPush=Push(startX,y);
if(bPush)
...{
stack.Push(new Point(startX,y));
y++;
startX=0;
if(stack.Count==8)
...{
Stack copyStack=(Stack)stack.Clone();
for(int i=0;i<places.Length;i++)
...{
places[i]=(Point)copyStack.Pop();
}
// if(total.Contains(places))
// {//如果已经存在
// break;//跳出循环
// }
string sss=null;
foreach(Point ps in places)
...{
sss+=(ps.ToString()+" ");
}
// Console.WriteLine(sss);
total.Add(places);
places=new Point[8];
Point p=(Point)stack.Pop();
startX=p.X;
y=p.Y;
cancelEffect(startX,y,1);
QiPan[startX,y]=1;
startX++;
}
}
else if(startX>=8)
...{
if(stack.Count==0)
...{
break;
}
Point p=(Point)stack.Pop();
y=p.Y;
startX=p.X;
//
cancelEffect(startX,y,1);
QiPan[startX,y]=1;
startX++;
// coutQiPan();
}
else if(!bPush && startX<8)
...{
startX++;
}
// coutQiPan();
}
}
private static void coutQiPan()
...{/**/////this funcation is used to debug
Console.WriteLine("棋盘:");
for(int i=0;i<8;i++)
...{
for(int j=0;j<8;j++)
...{
Console.Write(QiPan[j,i].ToString(" 0;-#;")+" ");
}
Console.Write(" ");
}
}
private static void forcancel(int [,] qp,int x,int y,int row,int setValue)
...{
if(qp[x,y]==(0-row))
qp[x,y]=setValue;
}
public static void cancelEffect(int x,int y,int flag)//flag=1
...{
for(int i=0;i<8;i++)
...{/**////将横和竖标记为不可以放棋子了
// if(QiPan[x,i]==(0-y))
// QiPan[x,i]=flag;
forcancel(QiPan,x,i,y,flag);
forcancel(QiPan,i,y,y,flag);
//if(QiPan[y,i]=flag;
}
//下面将斜位置标记为不可以放棋子
int n=0;
while(x-n>=0 && y-n>=0) //将左斜上方
...{
forcancel(QiPan,x-n,y-n,y,flag);
//QiPan[x-n,y-n]=flag;
n++;
}
n=0;
while(x+n<8 && y+n<8) //右下
...{
forcancel(QiPan,x+n,y+n,y,flag);
//QiPan[x+n,y+n]=flag;
n++;
}
n=0;
int k=0;
while(x+n<8 && y-k>=0)//右上
...{
forcancel(QiPan,x+n,y-k,y,flag);
//QiPan[x+n,y-k]=flag;
n++;k++;
}
n=0;k=0;
while(x-n>=0 && y+k<8)//左下
...{
forcancel(QiPan,x-n,y+k,y,flag);
// QiPan[x-n,y+k]=flag;
n++;k++;
}
//
}
public static bool canPush(int x,int y)
...{
if(x<0 || x>=8 || y<0 || y>=8)return false;
return (QiPan[x,y]<=0 || QiPan[x,y]==9 )?false:true;
}
private static void fortag(int[,] qp,int x,int y,int flag)
...{
if(qp[x,y]==1)
qp[x,y]=flag;
}
public static void tag(int x,int y,int flag)
...{
flag=-y;
for(int i=0;i<8;i++)
...{/**////将横和竖标记为不可以放棋子了
// QiPan[x,i]=flag;
// QiPan[i,y]=flag;
fortag(QiPan,x,i,flag);
fortag(QiPan,i,y,flag);
}
//下面将斜位置标记为不可以放棋子
int n=0;
while(x-n>=0 && y-n>=0) //将左斜上方
...{
// QiPan[x-n,y-n]=flag;
fortag(QiPan,x-n,y-n,flag);
n++;
}
n=0;
while(x+n<8 && y+n<8) //右下
...{
// QiPan[x+n,y+n]=flag;
fortag(QiPan,x+n,y+n,flag);
n++;
}
n=0;
int k=0;
while(x+n<8 && y-k>=0)//右上
...{
// QiPan[x+n,y-k]=flag;
fortag(QiPan,x+n,y-k,flag);
n++;k++;
}
n=0;k=0;
while(x-n>=0 && y+k<8)//左下
...{
// QiPan[x-n,y+k]=flag;
fortag(QiPan,x-n,y+k,flag);
n++;k++;
}
//
}
public static bool Push(int x,int y)
...{
if(!canPush(x,y))
...{
// MessageBox.Show("shit");
return false;
}
tag(x,y,-1);//标记出棋盘上不能放棋子的位置
QiPan[x,y]=9;//将当前位置标记为已经放入棋子
return true;
}
private void button1_Click(object sender, System.EventArgs e)
...{
int current=Int32.Parse(label1.Text);
if(current>=total.Count-1)
return;
int now=current+1;
label1.Text=now.ToString();
this.Refresh();
}
private void button2_Click(object sender, System.EventArgs e)
...{
int current=Int32.Parse(label1.Text);
if(current<=0)
return;
int now=current-1;
label1.Text=now.ToString();
this.Refresh();
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
...{
int current=Int32.Parse(comboBox1.Text);
if(current>=total.Count || current<0)
return;
int now=current;
label1.Text=now.ToString();
this.Refresh();
}
}
}