publicbool Exist(State S) ...{ bool isexist =false; if (CClosed[S.hashvalue] !='
下面是程序全部:
publicclass BaShuMa ...{ publicint DEPTH =1; publicint AllCount=0; publicchar[] CClosed =newchar[100000000]; publicclass myReverserClass : IComparer ...{ // Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer.Compare(Object x, Object y) ...{ return ((new CaseInsensitiveComparer()).Compare(((State)y).Dis, ((State)x).Dis)); } } publicint MIN =100; publicconstint MAX =9; publicconstint N =3; public ArrayList Opened =new ArrayList(); public ArrayList Closed =new ArrayList(); /**////<summary> /// 当前节点是否是目标状态 ///</summary> ///<param name="S"></param> ///<returns></returns> publicbool IsAnswer(State S) ...{ if (S.hashvalue ==12345678) ...{ returntrue; } else returnfalse; } /**////<summary> /// 搜索当前节点的子状态 ///</summary> ///<param name="S"></param> ///<param name="P"></param> ///<returns></returns> public ArrayList SearchNextState(State S, int P) ...{ ArrayList ar =new ArrayList(); int row = P / N; int col = P % N; State tt; if (row >0) ...{ tt =new State(); tt.A =newint[MAX]; S.A.CopyTo(tt.A, 0); int t = tt.A[(row -1) * N + col]; tt.A[(row -1) * N + col] = tt.A[P]; tt.A[P] = t; tt.Index = (row -1) * N + col; tt.Dis = JiSuanDis(tt); tt.hashvalue = GetHashValue(tt); if (!Exist(tt)) ...{ tt.Depth = S.Depth +1; if (tt.Depth <= DEPTH) ...{ tt.last = S; ar.Add(tt); } } } if (row < N -1) ...{ tt =new State(); tt.A =newint[MAX]; S.A.CopyTo(tt.A, 0); int t = tt.A[(row +1) * N + col]; tt.A[(row +1) * N + col] = tt.A[P]; tt.A[P] = t; tt.Index = (row +1) * N + col; tt.Dis = JiSuanDis(tt); tt.hashvalue = GetHashValue(tt); if (!Exist(tt)) ...{ tt.Depth = S.Depth +1; if (tt.Depth <= DEPTH) ...{ tt.last = S; ar.Add(tt); } } } if (col >0) ...{ tt =new State(); tt.A =newint[MAX]; S.A.CopyTo(tt.A, 0); int t = tt.A[row * N + col -1]; tt.A[row * N + col -1] = tt.A[P]; tt.A[P] = t; tt.Index = row * N + col -1; tt.Dis = JiSuanDis(tt); tt.hashvalue = GetHashValue(tt); if (!Exist(tt)) ...{ tt.Depth = S.Depth +1; if (tt.Depth <= DEPTH) ...{ tt.last = S; ar.Add(tt); } } } if (col < N -1) ...{ tt =new State(); tt.A =newint[MAX]; S.A.CopyTo(tt.A, 0); int t = tt.A[row * N + col +1]; tt.A[row * N + col +1] = tt.A[P]; tt.A[P] = t; tt.Index = row * N + col +1; tt.Dis = JiSuanDis(tt); tt.hashvalue = GetHashValue(tt); if (!Exist(tt)) ...{ tt.Depth = S.Depth +1; if (tt.Depth <= DEPTH) ...{ tt.last = S; ar.Add(tt); } } } IComparer myComparer =new myReverserClass(); ar.Sort(myComparer); return ar; } /**////<summary> /// 搜索八数码 ///</summary> ///<param name="S"></param> ///<returns></returns> public State Search(State S) ...{ for (int i =0; i < S.A.Length; i++) ...{ if (S.A[i] == MAX) S.Index = i; } int PermS =0; for (int i =0; i <9; i++) ...{ for (int j =0; j < i; j++) ...{ if (S.A[j] > S.A[i] && S.A[i] !=9&&S.A[j]!=9) PermS++; } } if (PermS %2!=0) ...{ Console.WriteLine("无解"); //return null; } S.Depth =1; S.hashvalue=GetHashValue(S); Opened.Add(S); State T; while (DEPTH <100) ...{ Opened.Clear(); Opened.Add(S); AllCount =0; CClosed =newchar[100000000]; while (Opened.Count >0) ...{ T = (State)Opened[Opened.Count -1]; Opened.RemoveAt(Opened.Count -1); CClosed[T.hashvalue] ='y'; AllCount++; if (IsAnswer(T)) ...{ Print(T); return T; } ArrayList Tar = SearchNextState(T, T.Index); foreach (State t in Tar) ...{ Opened.Add(t); } } DEPTH++; } returnnull; } /**////<summary> /// 判断Closed表是否已存在 ///</summary> ///<param name="S"></param> ///<returns></returns> publicbool Exist(State S) ...{ bool isexist =false; if (CClosed[S.hashvalue] !='
主函数:
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace 八数码 ...{ class Program ...{ staticvoid Main(string[] args) ...{ BaShuMa bashuma =new BaShuMa(); State S =new State(); A =newint[9] ; Console.WriteLine("请输入初始状态:"); int j =0; while (j <9) ...{ string str = Console.ReadLine(); char[] c =newchar[1] ...{ '' }; String[] StrAr = str.Split(c); for (int i =0; i < StrAr.Length; i++) ...{ if (StrAr[i] !="") ...{ A[j++] =int.Parse(StrAr[i]); if (j >=9) break; } } } S.A = A; bashuma.Search(S); Console.ReadLine(); } } }