📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 算法分析_棋盘覆盖
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label5;
/// <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 );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(424, 112);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(24, 24);
this.label1.TabIndex = 0;
this.label1.Text = "2";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(472, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(16, 16);
this.label2.TabIndex = 1;
this.label2.Text = "*";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(496, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(24, 24);
this.label3.TabIndex = 2;
this.label3.Text = "2";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(440, 96);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(24, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(512, 96);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(24, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// label4
//
this.label4.Location = new System.Drawing.Point(424, 56);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(128, 24);
this.label4.TabIndex = 5;
this.label4.Text = "请选择棋盘的规模";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// button1
//
this.button1.Location = new System.Drawing.Point(448, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 32);
this.button1.TabIndex = 6;
this.button1.Text = "开始绘制";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(448, 296);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 32);
this.button2.TabIndex = 7;
this.button2.Text = "重新绘制";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(448, 232);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(80, 32);
this.button3.TabIndex = 8;
this.button3.Text = "开始覆盖";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(512, 136);
this.textBox3.Name = "textBox3";
this.textBox3.ReadOnly = true;
this.textBox3.Size = new System.Drawing.Size(24, 21);
this.textBox3.TabIndex = 9;
this.textBox3.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(408, 136);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(96, 32);
this.label5.TabIndex = 10;
this.label5.Text = "该棋盘的规模是行列各为";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 406);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "棋盘覆盖问题 夏琛02123003";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
try
{
if(textBox1.Text!="")
{
int.Parse(textBox1.Text);
textBox2.Text=textBox1.Text;
}
}
catch(System.Exception ee)
{
MessageBox.Show("Error! 类型错","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
textBox1.Text="";
textBox2.Text="";
}
}
private void textBox2_TextChanged(object sender, System.EventArgs e)
{
try
{
if(textBox2.Text!="")
{
int.Parse(textBox2.Text);
textBox1.Text=textBox2.Text;
}
}
catch(System.Exception ee)
{
MessageBox.Show("Error! 类型错","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
textBox1.Text="";
textBox2.Text="";
}
}
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text==""&&textBox2.Text=="")
{
MessageBox.Show("空值无法绘制!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
textBox1.ReadOnly=true;
textBox2.ReadOnly=true;
Graphics g=CreateGraphics();//创建绘图
Pen blackPen = new Pen(Color.Black, 1);//创建画笔
int size_S=int.Parse(textBox1.Text);//记录下所需绘制的大小
int size_D=1;
for(int i=0;i<size_S;i++)//有几个2循环几次
{
size_D*=2;
}
textBox3.Text=size_D.ToString();
int round=400/size_D;//将冗余的线去除
int round_all=round*size_D;
for(int i=0;i<=round_all;i=i+round)//绘制棋盘
{
g.DrawLine(blackPen,0,i,round_all,i);
g.DrawLine(blackPen,i,0,i,round_all);
}
g.Dispose();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
if(textBox1.Text!=""&&textBox2.Text!="")
{
textBox1.ReadOnly=false;
textBox2.ReadOnly=false;
textBox1.Text="";
textBox2.Text="";
this.Refresh();
// this.Dispose();
}
else
{
MessageBox.Show("空值无需刷新!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
//此函数即为覆盖算法的函数
public void ChessBoard(int tr,int tc,int dr,int dc,int size,int length)
{
if(size==length)return;
int s=size/2;
//覆盖左上角棋盘
if(dr<tr+s&&dc<tc+s)//特殊方格在左上角
{
SolidBrush brush1=new SolidBrush(Color.Blue);
fill2(tr+s-length,tc+s,brush1);
fill2(tr+s,tc+s-length,brush1);
fill2(tr+s,tc+s,brush1);
ChessBoard(tr,tc,dr,dc,s,length);//左上
ChessBoard(tr,tc+s,tr+s-length,tc+s,s,length);//左下
ChessBoard(tr+s,tc,tr+s,tc+s-length,s,length);//右上
ChessBoard(tr+s,tc+s,tr+s,tc+s,s,length);//右下
}
/*else//棋盘中无特殊方格,用骨牌覆盖右下角
{
fill2(tr+s-length,tc+s-length,brush);
// Board[tr+s-1,tc+s-1]=t;
ChessBoard(tr,tc,tr+s-length,tc+s-length,s,length);//覆盖其余方格
}*/
//覆盖右上角棋盘
if(dr<tr+s&&dc>=tc+s)//特殊方格在左下角
{
SolidBrush brush2=new SolidBrush(Color.Red);
fill2(tr+s-length,tc+s-length,brush2);
fill2(tr+s,tc+s-length,brush2);
fill2(tr+s,tc+s,brush2);
ChessBoard(tr,tc+s,dr,dc,s,length);
ChessBoard(tr,tc,tr+s-length,tc+s-length,s,length);
ChessBoard(tr+s,tc,tr+s,tc+s-length,s,length);
ChessBoard(tr+s,tc+s,tr+s,tc+s,s,length);
}
/*else//棋盘中无特殊方格,用骨牌覆盖左下角
{
fill2(tr+s-length,tc+s,brush);
// Board[tr+s-1,tc+s]=t;
ChessBoard(tr,tc+s,tr+s-length,tc+s,s,length);//覆盖其余方格
}*/
//覆盖左下角棋盘
if(dr>=tr+s&&dc<tc+s)//右上
{
SolidBrush brush3=new SolidBrush(Color.Yellow);
fill2(tr+s-length,tc+s-length,brush3);
fill2(tr+s-length,tc+s,brush3);
fill2(tr+s,tc+s,brush3);
ChessBoard(tr+s,tc,dr,dc,s,length);
ChessBoard(tr,tc,tr+s-length,tc+s-length,s,length);
ChessBoard(tr,tc+s,tr+s-length,tc+s,s,length);
ChessBoard(tr+s,tc+s,tr+s,tc+s,s,length);
}//特殊方格在左下角
/*else//棋盘中无特殊方格,用骨牌覆盖右上角
{
fill2(tr+s,tc+s-length,brush);
// Board[tr+s,tc+s-1]=t;
ChessBoard(tr+s,tc,tr+s,tc+s-length,s,length);//覆盖其余方格
}*/
//覆盖右下角棋盘
if(dr>=tr+s&&dc>=tc+s)
{
SolidBrush brush4=new SolidBrush(Color.Green);
fill2(tr+s-length,tc+s-length,brush4);//特殊方格在右下角
fill2(tr+s-length,tc+s,brush4);
fill2(tr+s,tc+s-length,brush4);
ChessBoard(tr+s,tc+s,dr,dc,s,length);
ChessBoard(tr,tc,tr+s-length,tc+s-length,s,length);
ChessBoard(tr,tc+s,tr+s-length,tc+s,s,length);
ChessBoard(tr+s,tc,tr+s,tc+s-length,s,length);
}
/*else//棋盘中无特殊方格,用骨牌覆盖左上角
{
fill2(tr+s,tc+s,brush);
// Board[tr+s,tc+s]=t;
ChessBoard(tr+s,tc+s,tr+s,tc+s,s,length);//覆盖其余方格
}*/
}
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.X>400||e.Y>400)
{
if(textBox1.Text==""&&textBox2.Text=="")
{
MessageBox.Show("空值,请输入!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if(textBox1.ReadOnly==false&&textBox2.ReadOnly==false)
{
MessageBox.Show("还未绘制棋盘!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else
{
int i=e.X;
int j=e.Y;
SolidBrush myBrush= new SolidBrush(Color.Black);
fill(i,j,myBrush);
}
}
static int x_first=-1;//记录初始点的位置
static int y_first=-1;
public void fill2(int x,int y,SolidBrush brush)
{
int size_S=int.Parse(textBox1.Text);//记录下所需绘制的大小
int size_D=1;
for(int i=0;i<size_S;i++)//有几个2循环几次
{
size_D*=2;
}
int round=400/size_D;
Graphics point = CreateGraphics();
point.FillRectangle(brush,new Rectangle(x+1,y+1,round-1,round-1));
// point.Dispose();
}
public void fill(int x,int y,SolidBrush brush)
{
int size_S=int.Parse(textBox1.Text);//记录下所需绘制的大小
int size_D=1;
for(int i=0;i<size_S;i++)//有几个2循环几次
{
size_D*=2;
}
int round=400/size_D;
int round_all=round*size_D;
int x_now=(x/round)*round;
int y_now=(y/round)*round;
x_first=x_now;//记录下起始点的行号和列号
y_first=y_now;
// textBox4.Text=x_first.ToString()+y_first.ToString();
Graphics point = CreateGraphics();
point.FillRectangle(brush,new Rectangle(x_now+1,y_now+1,round-1,round-1));
// brush.Dispose();
point.Dispose();
}
private void button3_Click(object sender, System.EventArgs e)
{
if(textBox1.Text==""&&textBox2.Text=="")
{
MessageBox.Show("空值,请输入!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if(textBox1.ReadOnly==false&&textBox2.ReadOnly==false)
{
MessageBox.Show("还未绘制棋盘!","ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if(x_first==-1&&y_first==-1)
{
MessageBox.Show("初始点还未选定,请选择!","ERROR!",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
int size_S=int.Parse(textBox1.Text);//记录下所需绘制的大小
int size_D=1;
for(int i=0;i<size_S;i++)//有几个2循环几次
{
size_D*=2;
}
int round=400/size_D;
int round_all=round*size_D;
ChessBoard(0,0,x_first,y_first,round_all,round);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -