第二题:数字方阵,九宫格
1) 阿拉伯数字 1-9 中选择一个数字填入下图中的空白方格内,每格只能填一个数字
2) 当每个格子内都有一个数字后,每行都不可以有重复的数字,每列也不可以有重复的数字
初始界面
![](https://www.cnblogs.com/images/cnblogs_com/zhoukun921/jgg1.jpg)
运行后结果
![](https://www.cnblogs.com/images/cnblogs_com/zhoukun921/jgg2.jpg)
2) 当每个格子内都有一个数字后,每行都不可以有重复的数字,每列也不可以有重复的数字
初始界面
![](https://www.cnblogs.com/images/cnblogs_com/zhoukun921/jgg1.jpg)
运行后结果
![](https://www.cnblogs.com/images/cnblogs_com/zhoukun921/jgg2.jpg)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace SortNum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AllData[0, 3] = 7;
AllData[0, 4] = 2;
AllData[0, 5] = 8;
AllData[1, 1] = 9;
AllData[1, 4] = 5;
AllData[1, 5] = 1;
AllData[1, 6] = 6;
AllData[2, 4] = 6;
AllData[2, 7] = 8;
AllData[2, 8] = 2;
AllData[3, 0] = 3;
AllData[3, 3] = 8;
AllData[3, 5] = 2;
AllData[3, 6] = 7;
AllData[3, 8] = 4;
AllData[4, 0] = 1;
AllData[4, 1] = 7;
AllData[4, 2] = 4;
AllData[4, 4] = 3;
AllData[4, 7] = 2;
AllData[5, 0] = 2;
AllData[5, 1] = 8;
AllData[5, 3] = 5;
AllData[5, 7] = 3;
AllData[6, 1] = 1;
AllData[6, 3] = 3;
AllData[6, 6] = 2;
AllData[7, 2] = 7;
AllData[7, 4] = 4;
AllData[7, 5] = 6;
AllData[7, 8] = 5;
AllData[8, 2] = 6;
AllData[8, 3] = 1;
AllData[8, 7] = 4;
AllData[8, 8] = 9;
int writelen = 0;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (AllData[i, j] == 0)
{
writelen++;
}
}
}
CanWritePoint = new Apoint[writelen];//可填数字的空白区
writelen = 0;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (AllData[i, j] == 0)
{
CanWritePoint[writelen].row = i;
CanWritePoint[writelen].column = j;
writelen++;
}
}
}
}
int ResultCount = 0;
static int[,] AllData = new int[9, 9];
int[] ARow = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Apoint[] CanWritePoint;//可以填数字的点
struct Apoint
{
public int row;
public int column;
}
private void button1_Click(object sender, EventArgs e)//GO
{
Begain(0);
ResultNum.Text = ResultCount.ToString();
ShowResult(AllRe[0]);
}
private void Begain(int NowPoint)
{
List<int> num = GetPossibleValue(CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column);
if (num.Count < 1)//此位置不能填任何数字了
{
return;
}
if (NowPoint == CanWritePoint.Length - 1)//最后一个可填写区了
{
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = num[0];
ResultCount++;
SaveResult();
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = 0;
return;
}
List<int>.Enumerator enu = num.GetEnumerator();
while (enu.MoveNext())
{
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = enu.Current;
Begain(NowPoint + 1);
}
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = 0;
}
private List<int> GetPossibleValue(int row, int column)//得到此位置能填的数字
{
List<int> re = new List<int>();
re.AddRange(ARow);
List<int> temp = new List<int>();
for (int i = 0; i < 9; i++)
{
if (AllData[row, i] != 0)
{
temp.Add(AllData[row, i]);
}
if (AllData[i, column] != 0)
{
temp.Add(AllData[i, column]);
}
}
List<int>.Enumerator ie = temp.GetEnumerator();
while (ie.MoveNext())
{
re.Remove(ie.Current);
}
return re;
}
private List<int[,]> AllRe = new List<int[,]>();//保存所有结果
private void SaveResult()//保存结果
{
int[,] temp = new int[9,9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
temp[i, j] = AllData[i, j];
}
}
AllRe.Add(temp);
}
//************************************************************
//下面与算法无关,为了显示结果用的
int now = 0;
private void button2_Click(object sender, EventArgs e)//上一个
{
if (now == 0)
{
MessageBox.Show("已到端点");
return;
}
now--;
ShowResult(AllRe[now]);
page.Text = (now+1).ToString();
}
private void button3_Click(object sender, EventArgs e)//下一个
{
if (now == AllRe.Count - 1)
{
MessageBox.Show("已到最后");
return;
}
now++;
ShowResult(AllRe[now]);
page.Text = (now + 1).ToString();
}
private void ShowResult(int[,] pr)//打印结果
{
int row = 0;
int column = 0;
foreach (Control co in tlp.Controls)
{
if (co.Name.StartsWith("lbl"))
{
Label la = co as Label;
row = Convert.ToInt32(co.Name.Substring(3, 1));
column = Convert.ToInt32(co.Name.Substring(4));
la.Text = pr[row, column].ToString();
}
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace SortNum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
AllData[0, 3] = 7;
AllData[0, 4] = 2;
AllData[0, 5] = 8;
AllData[1, 1] = 9;
AllData[1, 4] = 5;
AllData[1, 5] = 1;
AllData[1, 6] = 6;
AllData[2, 4] = 6;
AllData[2, 7] = 8;
AllData[2, 8] = 2;
AllData[3, 0] = 3;
AllData[3, 3] = 8;
AllData[3, 5] = 2;
AllData[3, 6] = 7;
AllData[3, 8] = 4;
AllData[4, 0] = 1;
AllData[4, 1] = 7;
AllData[4, 2] = 4;
AllData[4, 4] = 3;
AllData[4, 7] = 2;
AllData[5, 0] = 2;
AllData[5, 1] = 8;
AllData[5, 3] = 5;
AllData[5, 7] = 3;
AllData[6, 1] = 1;
AllData[6, 3] = 3;
AllData[6, 6] = 2;
AllData[7, 2] = 7;
AllData[7, 4] = 4;
AllData[7, 5] = 6;
AllData[7, 8] = 5;
AllData[8, 2] = 6;
AllData[8, 3] = 1;
AllData[8, 7] = 4;
AllData[8, 8] = 9;
int writelen = 0;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (AllData[i, j] == 0)
{
writelen++;
}
}
}
CanWritePoint = new Apoint[writelen];//可填数字的空白区
writelen = 0;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (AllData[i, j] == 0)
{
CanWritePoint[writelen].row = i;
CanWritePoint[writelen].column = j;
writelen++;
}
}
}
}
int ResultCount = 0;
static int[,] AllData = new int[9, 9];
int[] ARow = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Apoint[] CanWritePoint;//可以填数字的点
struct Apoint
{
public int row;
public int column;
}
private void button1_Click(object sender, EventArgs e)//GO
{
Begain(0);
ResultNum.Text = ResultCount.ToString();
ShowResult(AllRe[0]);
}
private void Begain(int NowPoint)
{
List<int> num = GetPossibleValue(CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column);
if (num.Count < 1)//此位置不能填任何数字了
{
return;
}
if (NowPoint == CanWritePoint.Length - 1)//最后一个可填写区了
{
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = num[0];
ResultCount++;
SaveResult();
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = 0;
return;
}
List<int>.Enumerator enu = num.GetEnumerator();
while (enu.MoveNext())
{
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = enu.Current;
Begain(NowPoint + 1);
}
AllData[CanWritePoint[NowPoint].row, CanWritePoint[NowPoint].column] = 0;
}
private List<int> GetPossibleValue(int row, int column)//得到此位置能填的数字
{
List<int> re = new List<int>();
re.AddRange(ARow);
List<int> temp = new List<int>();
for (int i = 0; i < 9; i++)
{
if (AllData[row, i] != 0)
{
temp.Add(AllData[row, i]);
}
if (AllData[i, column] != 0)
{
temp.Add(AllData[i, column]);
}
}
List<int>.Enumerator ie = temp.GetEnumerator();
while (ie.MoveNext())
{
re.Remove(ie.Current);
}
return re;
}
private List<int[,]> AllRe = new List<int[,]>();//保存所有结果
private void SaveResult()//保存结果
{
int[,] temp = new int[9,9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
temp[i, j] = AllData[i, j];
}
}
AllRe.Add(temp);
}
//************************************************************
//下面与算法无关,为了显示结果用的
int now = 0;
private void button2_Click(object sender, EventArgs e)//上一个
{
if (now == 0)
{
MessageBox.Show("已到端点");
return;
}
now--;
ShowResult(AllRe[now]);
page.Text = (now+1).ToString();
}
private void button3_Click(object sender, EventArgs e)//下一个
{
if (now == AllRe.Count - 1)
{
MessageBox.Show("已到最后");
return;
}
now++;
ShowResult(AllRe[now]);
page.Text = (now + 1).ToString();
}
private void ShowResult(int[,] pr)//打印结果
{
int row = 0;
int column = 0;
foreach (Control co in tlp.Controls)
{
if (co.Name.StartsWith("lbl"))
{
Label la = co as Label;
row = Convert.ToInt32(co.Name.Substring(3, 1));
column = Convert.ToInt32(co.Name.Substring(4));
la.Text = pr[row, column].ToString();
}
}
}
}
}
posted on 2008-05-17 19:22 Handsome Kun 阅读(2657) 评论(0) 编辑 收藏 举报