Handsome Kun

仰天长笑,笑天下可笑之人; 大肚能盛,盛天下可盛之事!

导航

第二题:数字方阵,九宫格

1) 阿拉伯数字 1-9 中选择一个数字填入下图中的空白方格内,每格只能填一个数字
2) 当每个格子内都有一个数字后,每行都不可以有重复的数字,每列也不可以有重复的数字
初始界面

运行后结果

      

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[
03= 7;
            AllData[
04= 2;
            AllData[
05= 8;

            AllData[
11= 9;
            AllData[
14= 5;
            AllData[
15= 1;
            AllData[
16= 6;

            AllData[
24= 6;
            AllData[
27= 8;
            AllData[
28= 2;

            AllData[
30= 3;
            AllData[
33= 8;
            AllData[
35= 2;
            AllData[
36= 7;
            AllData[
38= 4;

            AllData[
40= 1;
            AllData[
41= 7;
            AllData[
42= 4;
            AllData[
44= 3;
            AllData[
47= 2;

            AllData[
50= 2;
            AllData[
51= 8;
            AllData[
53= 5;
            AllData[
57= 3;

            AllData[
61= 1;
            AllData[
63= 3;
            AllData[
66= 2;

            AllData[
72= 7;
            AllData[
74= 4;
            AllData[
75= 6;
            AllData[
78= 5;

            AllData[
82= 6;
            AllData[
83= 1;
            AllData[
87= 4;
            AllData[
88= 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[99];
        
int[] ARow = new int[9] { 123456789 };
        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(31));
                    column 
= Convert.ToInt32(co.Name.Substring(4));
                    la.Text 
= pr[row, column].ToString();
                }
            }
        }

    }
}

posted on 2008-05-17 19:22  Handsome Kun  阅读(2657)  评论(0编辑  收藏  举报