IDA*(迭代加深的A*算法) 八數碼

 人工智能的書也在講A*,正好順便學學


// darkscope.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;
struct node
{
   int state[3][3];
   int x,y,g;
   node()
   {memset(state,0,sizeof(state));
   x=0;y=0;g=0;
   }
   void show()
   {
        cout<<"--------"<<endl;
        for (int i=0;i<3;i++)
        {
        for (int j=0;j<3;j++)
          cout<<state[i][j]<<" ";
          cout<<endl;
        }       
        cout<<"--------"<<endl;
        }
       };
node getastate()
{
     node ans;
     for (int i=0;i<3;i++)
     for (int j=0;j<3;j++)
     {
     cin>>ans.state[i][j];
     if (ans.state[i][j]==0)
     {
                            ans.x=i;
                            ans.y=j;
                            }
     }
     return ans;
}
node src,tar;

int h_val(node a)
{
    int ans=0;
    for (int i=0;i<3;i++)
         for (int j=0;j<3;j++)
              ans+=(a.state[i][j]!=tar.state[i][j]);
    return ans;
}
bool same(node &a,node &b)
{
     for (int i=0;i<3;i++)
     for (int j=0;j<3;j++)
     if (a.state[i][j]!=b.state[i][j]) return false;
     return true;
     }
void swap(int &a,int &b)
{
     int t;
     t=a;
     a=b;
     b=t;
     }
void ida_star()
{
      int pathlimit=h_val(src)-1;
      bool success=false;  
      do
      {
        pathlimit+=1;
        src.g=0;
        stack<node> Stack;
        Stack.push(src);
        do
        {
           node current=Stack.top();
           Stack.pop();  
           if (same(current,tar))
             success=true;
             else
			if (pathlimit>=current.g + h_val(current))
             {
                  if (current.x>0)
                  {
                    node b=current;
                    b.g=current.g+1;
                    swap(b.state[b.x][b.y],b.state[b.x-1][b.y]);
                    b.x-=1;  
                    Stack.push(b);                               
                     }
                  if (current.x<2)
                  { 
                    node b=current;
                    b.g=current.g+1;
                    swap(b.state[b.x][b.y],b.state[b.x+1][b.y]);
                    b.x+=1; 
                    Stack.push(b);  
                                  }
                  if (current.y>0)
                  {
                    node b=current;
                    b.g=current.g+1;
                    swap(b.state[b.x][b.y],b.state[b.x][b.y-1]);
                    b.y-=1;
                    Stack.push(b);   
                                  }
                  if (current.y<2)
                  {
                    node b=current;
                    b.g=current.g+1;
                    swap(b.state[b.x][b.y],b.state[b.x][b.y+1]);
                    b.y+=1;
                    Stack.push(b);  
                                  }
                  }       
			      if (success) cout<<current.g<<endl;
        }
        while (!success && !Stack.empty());

	  }
      while (!success  && pathlimit<20);

}
int main()
{

    src=getastate();
    tar=getastate();
    ida_star();
    return 0 ;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章