前段时间看到说有人设计了个很难的独数,花了在个月时间,人为解肯定是很复杂,但是我们程序猿可以用代码来解解看.之前也没有把代码放上来.主要是用递归,其它递归这个j8是很耗内存的.
今天又看到一园友的博客写独数据算法,我粗看了一下,也没细读代码,看到说解第三个独数要用10秒,我本来想试试我的代码,结果我看博主的独数就是我以前用的,所以数据也不用改直接运行,感觉还是很快啊.不会用10称吧.后来加了时间看效果,0.9秒就搞定了啊.回复了楼主,说了要贴代码出来,所以下面直接不啰嗦,上代码.代码很乱,我现在也看不懂了.只是有运行结果.
C#的一坨代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Data;
using ConsoleApplication1.Class1;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Num n = new Num();
//n.SetPosition(5, 2);
//ArrayList al = n.GetNum();
//n.Print();
//n.Foo(0); //直接用这个会溢出,因为数据量操作太大了
n.sw.Start();
var t = new Thread(() => { n.Foo(0); }, 1073741824);
t.Start();
Console.Read();
}
}
public class Num
{
public System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
private int row;
private int col;
public void SetPosition(int r, int c)
{
row = r;
col = c;
}
private int[,] a = new int[9, 9] {
{8,0,0,0,0,0,0,0,0},
{0,0,3,6,0,0,0,0,0},
{0,7,0,0,9,0,2,0,0},
{0,5,0,0,0,7,0,0,0},
{0,0,0,0,4,5,7,0,0},
{0,0,0,1,0,0,0,3,0},
{0,0,1,0,0,0,0,6,8},
{0,0,8,5,0,0,0,1,0},
{0,9,0,0,0,0,4,0,0}
};
//栈记录可以填写的数据
private Stack<TryData> stack = new Stack<TryData>();
//打印
public void Print()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
Console.Write(a[i, j] + " ");
if (j % 3 == 2)
Console.Write(" ");
}
Console.WriteLine();
if (i % 3 == 2)
Console.WriteLine();
}
Console.WriteLine("time:" + sw.Elapsed);
}
public ArrayList GetNum()
{
//List<int> n = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ArrayList n = new ArrayList() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//行
for (int i = 0; i < 9; i++)
{
n.Remove(a[row, i]);
}
//列
for (int i = 0; i < 9; i++)
{
n.Remove(a[i, col]);
}
//当前九宫格
int[] r = new int[3];
int[] c = new int[3];
if (row % 3 == 0)
{
//第一行
r[0] = row;
r[1] = row + 1;
r[2] = row + 2;
}
else if (row % 3 == 1)
{
//第二行
r[0] = row - 1;
r[1] = row;
r[2] = row + 1;
}
else
{
//第三行
r[0] = row - 2;
r[1] = row - 1;
r[2] = row;
}
//r[row % 3] = row;
//r[(row % 3 + 1) % 3] =0;
//r[(row % 3 + 2) % 3] =0;
if (col % 3 == 0)
{
c[0] = col;
c[1] = col + 1;
c[2] = col + 2;
}
else if (col % 3 == 1)
{
c[0] = col - 1;
c[1] = col;
c[2] = col + 1;
}
else
{
c[0] = col - 2;
c[1] = col - 1;
c[2] = col;
}
for (int i = 0; i < r.Length; i++)
{
for (int j = 0; j < c.Length; j++)
{
n.Remove(a[r[i], c[j]]);
}
}
return n;
}
public void Foo(int back)
{
if (back == 1)
{
//回退情况
if (stack.Count > 0)
{
TryData d = stack.Pop();
if (d.Row == row && d.Col == col)
{
if (d.N >= d.Al.Count)
{
//清理返回
a[row, col] = 0;
Backward();
Foo(1);
}
else
{
a[row, col] = Convert.ToInt32(d.Al[d.N]);//试下一个数
d.N++;
stack.Push(d);
//前进
Forward();
Foo(0);
}
}
else
{
stack.Push(d);
Backward();
Foo(1);
}
}
else
{
Console.WriteLine("stack over");
return;
}
}
else
{
//前进情况
#region
if (a[row, col] == 0)
{
//试数
TryData td = new TryData();
td.Al = GetNum();
if (td.Al.Count == 0)
{
//清理返回
a[row, col] = 0;
Backward();
Foo(1);
}
else
{
td.Row = row;
td.Col = col;
a[row, col] = Convert.ToInt32(td.Al[td.N]);//试下一个数
td.N++;
stack.Push(td);
//前进
Forward();
Foo(0);
}
}
else
{
Forward();
Foo(0);
}
#endregion
}
}
//前进
private void Forward()
{
col++;
if (col >= 9)
{
col = 0;
row++; //Console.WriteLine("+=>{0} 行", row);
if (row >= 9)
{
//Console.WriteLine("Last Row");
Print();
sw.Stop();
return;
}
}
}
//后退
private void Backward()
{
col--;
if (col < 0)
{
col = 8;
row--; //Console.WriteLine("-=>{0} 行", row);
if (row < 0)
{
Console.WriteLine("First Row");
return;
}
}
}
}
public class TryData
{
//这个数据在棋盘上的位置
public int Row = 0;
public int Col = 0;
//当前取的第几个
public int N = 0;
//这个空符合的数据
public ArrayList Al { get; set; }
}
}
上一张运行的结果图片
基本上几次都是0.9秒左右