C#实现八数码的IDEA*(迭代加深A*)算法

来源:互联网 发布:时尚大码女装淘宝店 编辑:程序博客网 时间:2024/05/22 01:28

八数码是一个经典的人工智能难题,因为好的算法在八数码问题中将搜索的几种经典方法体现的淋漓尽致,经过几种算法的比较,我发现用迭带加深的启发示算法的效率最好,而且求得解为最优解.

将Cloesd存储在一个100000000大小的 字符数组中,这样,数组中每一个元素对应一个状态序列,有点类似哈希存储,别看这摸简单的哈希函数,它可是其中的英雄!,有了它判重的时间复杂度变为O(1),于是就使八数码的30多万的状态空间能在30秒中搜索完毕!下面是该函数:

public long GetHashValue(State S)
        
{
            
return (S.A[0* 10000000 + S.A[1* 1000000 + S.A[2* 100000 + S.A[3* 10000 + S.A[4* 1000 + S.A[5* 100 + S.A[6* 10 + S.A[7* 1);
        }
         

判重函数:

public bool Exist(State S)
        
{
            
bool isexist = false;
            
if (CClosed[S.hashvalue] != '

下面是程序全部:

 public class BaShuMa
    
{
        
public int DEPTH = 1
        
public int   AllCount=0;
        
public char[] CClosed = new char[100000000];
        
public class 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));
            }

        }

        
public int MIN = 100;
        
public const int MAX = 9;
        
public const int N = 3;
        
public ArrayList Opened = new ArrayList();
        
public ArrayList Closed = new ArrayList();
        
/// <summary>
        
/// 当前节点是否是目标状态
        
/// </summary>
        
/// <param name="S"></param>
        
/// <returns></returns>

        public bool IsAnswer(State S)
        
{
            
if (S.hashvalue == 12345678)
            
{
                
return true;
            }

            
else
                
return false;
        }

        
/// <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 
= new int[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 
= new int[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 
= new int[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 
= new int[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 
= new char[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
++;
            }

            
return null;

        }

        
/// <summary>
        
/// 判断Closed表是否已存在
        
/// </summary>
        
/// <param name="S"></param>
        
/// <returns></returns>

        public bool 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
    
{          
        
static void Main(string[] args)
        
{

                 BaShuMa bashuma 
= new BaShuMa();
                State S 
= new State();
                A 
= new int[9] ;
                Console.WriteLine(
"请输入初始状态:");
                
int j = 0;
                
while (j < 9)
                
{
                    
string str = Console.ReadLine();
                    
char[] c = new char[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();                  
        }

    }

}


原创粉丝点击