问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespace自动演示皇后问题{publicpartialclassForm1:Form{publicintn;//输入的皇后个数publicfloatx;//画表格的横坐标publicfloaty;//画表格的纵坐标publicfloatd;//矩形间的间距publicboolflag;//判断合法或部分着色的返回值publicintk=0;//声明一个初始行数publicint[]c=newint[20];//声明一个数组,存放对应第k行的某一列privateImagetheImage;//要绘图的图片对象privateGraphicsg;//在图片上绘图的Graphics对象publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){//timer1.Enabled=true;//设置时间控件循环stringstr=textBox1.Text;if(str!=""){try{n=Int32.Parse(str);show();//画表格for(k=0;k<=n-1;k++)//初始化行列c[k]=0;k=0;}catch//异常处理{timer1.Enabled=false;MessageBox.Show("您的输入有误,请重新输入","提示");pictureBox1.Invalidate();pictureBox1.Update();textBox1.Clear();}}else{MessageBox.Show("请先输入皇后个数","消息框",MessageBoxButtons.OK,MessageBoxIcon.Warning);}}privatevoidshow()//用bitmap直线画表格{theImage=newBitmap(600,800);//宽,高g=Graphics.FromImage(theImage);g.Clear(Color.White);this.pictureBox1.Image=theImage;PenBluePen=newPen(Color.Black);//用直线画指定大小的矩形floatx=25.00F;floaty=25.00F;this.d=300.00F/n;for(inti=0;i<=n;i++){PointFX1=newPointF(x+i*d,y);PointFX2=newPointF(x+i*d,y+300.00F);g.DrawLine(BluePen,X1,X2);}x=25.00F;for(intj=0;j<=n;j++){PointFY1=newPointF(x,y+j*d);PointFY2=newPointF(x+300.00F,y+j*d);g.DrawLine(BluePen,Y1,Y2);}x=25.00F;y=25.00F;BluePen.Dispose();}publicboolcanput(intk)//判断合法着色{inti;for(i=0;i<k;i++){//如果k是最后一行,而且所有棋子不在同一列及同一对角线,则为合法着色if(k==n-1&&c[k]!=c[i]&&c[i]-c[k]!=i-k&&c[i]-c[k]!=k-i){flag=true;}else//否则为非法,跳出循环{flag=false;break;}}returnflag;//返回真值}publicboolpartput(intk)//判断部分着色{inti;for(i=0;i<k;i++){//如果并没有摆放完N个棋子,而且满足摆放的棋子不在同一列及同一对角线,则为部分着色if(c[k]!=c[i]&&c[i]-c[k]!=i-k&&c[i]-c[k]!=k-i){flag=true;}else//否则为非法,跳出循环{flag=false;break;}}returnflag;//返回真值}各位大哥,我是新手,我想在填充颜色的时候,窗口最小化的话,不会使颜色消失掉,但是不知道该怎么写这样的重绘,谢谢帮忙哈,还有部分代码,马上贴上来。
解决方案
解决方案二:
privatevoidbutton2_Click(objectsender,EventArgse)//执行下一步按钮{if(timer1.Enabled==true)//如果时间控件在循环,则提示用户{MessageBox.Show("请先按暂停键","提示");}else//{Graphicsgf=pictureBox1.CreateGraphics();//绘图对象SolidBrushmyBrush=newSolidBrush(Color.Red);//画刷SolidBrushmyBrush2=newSolidBrush(Color.White);//画刷if(k>=0){if(c[k]<=n-1){c[k]=c[k]+1;gf.FillRectangle(myBrush2,25.00F+(c[k]-2)*d+5,25.00F+k*d+5,d-10,d-10);gf.FillRectangle(myBrush,25.00F+(c[k]-1)*d+5,25.00F+k*d+5,d-10,d-10);if(k==0)//第一行为部分着色{gf.FillRectangle(myBrush,25.00F+(c[k]-1)*d+5,25.00F+k*d+5,d-10,d-10);k=k+1;}else{if(canput(k))//当为合法着色{MessageBox.Show("恭喜输出正确结果","消息框");}elseif(partput(k))//当为部分着色{k=k+1;}}}else{gf.FillRectangle(myBrush2,25.00F+(c[k]-1)*d+5,25.00F+k*d+5,d-10,d-10);c[k]=0;k-=1;}}else{if(n==0||n==2||n==3){MessageBox.Show("此问题无解","提示");timer1.Dispose();}else{MessageBox.Show("全部过程演示完毕","提示");timer1.Dispose();return;}}myBrush.Dispose();myBrush2.Dispose();}}///<summary>///时间控件,执行与“下一步”按钮相同的操作///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidtimer1_Tick(objectsender,EventArgse){Graphicsgf=pictureBox1.CreateGraphics();SolidBrushmyBrush=newSolidBrush(Color.Red);SolidBrushmyBrush2=newSolidBrush(Color.White);if(k>=0){if(c[k]<=n-1){c[k]=c[k]+1;gf.FillRectangle(myBrush2,25.00F+(c[k]-2)*d+5,25.00F+k*d+5,d-10,d-10);gf.FillRectangle(myBrush,25.00F+(c[k]-1)*d+5,25.00F+k*d+5,d-10,d-10);if(k==0)//第一行为部分着色{gf.FillRectangle(myBrush,25.00F+(c[k]-1)*d+5,25.00F+k*d+5,d-10,d-10);k=k+1;}else{if(canput(k))//当为合法着色{timer1.Enabled=false;MessageBox.Show("恭喜输出正确结果","消息框");}elseif(partput(k))//当为部分着色{k=k+1;}}}else{gf.FillRectangle(myBrush2,25.00F+(c[k]-1)*d+5,25.00F+k*d+5,d-10,d-10);c[k]=0;k-=1;}}else{if(n==0||n==2||n==3){timer1.Enabled=false;//计时器停止循环MessageBox.Show("此问题无解","提示");timer1.Dispose();return;}else{timer1.Enabled=false;MessageBox.Show("全部过程演示完毕","提示");timer1.Dispose();return;}}myBrush.Dispose();myBrush2.Dispose();}///<summary>///暂停///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton3_Click(objectsender,EventArgse){timer1.Enabled=false;}///<summary>///继续///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton4_Click(objectsender,EventArgse){timer1.Enabled=true;}}}
解决方案三:
友情up
解决方案四:
谢谢```唉,这个问题我也不知道怎么说,只好把代码都贴上来了,望大家海涵哈...
解决方案五:
如果觉得分不够,我可以加啊~只希望有人能解决噢```
解决方案六:
好象都没有正义之士啊~