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 ;
}

本文出自 “DarkScope从这里开始(..” 博客,请务必保留此出处http://darkscope.blog.51cto.com/4254649/992945

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值