8数码——宽度优先

#include <iostream>
#include<deque>
using namespace std;
class Eight_num{
  public:
    int a[3][3];
    Eight_num *pro;
    void display(){
        for(int i=0;i<3;i++){
         for(int j=0;j<3;j++)
            cout<<a[i][j]<<" ";
        cout<<endl;
      }
      //cout<<"my father node is :  "<<this.pro<<endl;
   }
   Eight_num(const Eight_num &one){   //deque  push 操作对自定义类的要求
        for(int i=0;i<3;i++){
         for(int j=0;j<3;j++)
            a[i][j]=one.a[i][j];
    }
     pro=one.pro;
  }
  Eight_num(){
       for(int i=0;i<3;i++){
         for(int j=0;j<3;j++)
            a[i][j]=0;
    pro=nullptr;
  }
  }
    void assignment(Eight_num &one){
        for(int i=0;i<3;i++){
         for(int j=0;j<3;j++)
            a[i][j]=one.a[i][j];
    }
  }
};




int findZero(Eight_num &eight_num);//找到交换的起始处
bool swapNum(Eight_num &eight_num,int place);
bool isGoal(Eight_num &eight_num,Eight_num &eight_num1);
bool isLook(Eight_num &eight);//查看是否已经存在
int start=0;


Eight_num eight;//init
deque<Eight_num>eight_deque;//deque
Eight_num goal_num;//goal


int main()
{   cout<<"Input the init state"<<endl;
    int num[9]={2,8,3,1,0,4,7,6,5};
    int t=0;
       for(int i=0;i<3;i++){
         for(int j=0;j<3;j++)
            {
                eight.a[i][j]=num[t++];
            }
    }
    eight_deque.push_back(eight);
    eight.display();
    cout<<"the  init  of  deque"<<endl;
    eight_deque[0].display();
    cout<<"the  init  of  goal  "<<endl;
    int num1[9]={1,2,3,8,0,4,7,6,5};
    t=0;
     for(int i=0;i<3;i++){
         for(int j=0;j<3;j++)
            {
                goal_num.a[i][j]=num1[t++];
            }
    }
    goal_num.display();
    //  above all right




    Eight_num itr_num=eight;//itr  用于遍历
    int place=0;
    bool bool1=isGoal(itr_num,goal_num);
    int size1;
    while(!bool1){
         place=findZero(itr_num);
         bool1=swapNum(itr_num,place);
         cout<<"  size= "<<eight_deque.size()<<endl;
         size1=eight_deque.size();
         if(start<size1)
            start++;
         else
         {
          cout<<"not find  goal"<<endl;
          break;
         }
         itr_num.assignment(eight_deque[start]);


    }


   return 0;
}
int findZero(Eight_num &eight_num){
    for(int i=0;i<3;i++)
       for(int j=0;j<3;j++){
         if(eight_num.a[i][j]==0)
            return 3*i+j;
    }
}
bool swapNum(Eight_num &eight_num,int place){
    Eight_num eight=eight_num;
    bool boo;
     /*eight.display();*/
    int temp;
    switch(place){
        case 0:
            temp=eight.a[0][0];
            eight.a[0][0]=eight.a[0][1];
            eight.a[0][1]=temp;
            boo=isLook(eight);
            if(boo)
                return 1;//查看是否已经在队列中存在,并且在插入后看是否为目标状态


            eight=eight_num;
            temp=eight.a[0][0];
            eight.a[0][0]=eight.a[1][0];
            eight.a[1][0]=temp;
            boo=isLook(eight);
            if(boo)
                return 1;
            break;
    case 1:
            temp=eight.a[0][1];
            eight.a[0][1]=eight.a[0][2];
            eight.a[0][2]=temp;
             boo=isLook(eight);
              if(boo)
                return 1;


            eight=eight_num;
            temp=eight.a[0][1];
            eight.a[0][1]=eight.a[0][0];
            eight.a[0][0]=temp;
             boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[0][1];
            eight.a[0][1]=eight.a[1][1];
            eight.a[1][1]=temp;
             boo=isLook(eight); if(boo)return 1;
            break;
       case 2:
            temp=eight.a[0][2];
            eight.a[0][2]=eight.a[1][2];
            eight.a[1][2]=temp;
            boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[0][2];
            eight.a[0][2]=eight.a[0][1];
            eight.a[1][0]=temp;
            boo=isLook(eight); if(boo)return 1;
            break;
        case 3:
            temp=eight.a[1][0];
            eight.a[1][0]=eight.a[1][1];
            eight.a[1][1]=temp;
            boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[1][0];
            eight.a[1][0]=eight.a[0][0];
            eight.a[0][0]=temp;
            boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[1][0];
            eight.a[1][0]=eight.a[2][0];
            eight.a[2][0]=temp;
            boo=isLook(eight); if(boo)return 1;
            break;
     case 4:
            temp=eight.a[1][1];
            eight.a[1][1]=eight.a[1][0];
            eight.a[1][0]=temp;
             boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[1][1];
            eight.a[1][1]=eight.a[1][2];
            eight.a[1][2]=temp;
            boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[1][1];
            eight.a[1][1]=eight.a[0][1];
            eight.a[0][1]=temp;
             boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[1][1];
            eight.a[1][1]=eight.a[2][1];
            eight.a[2][1]=temp;
             boo=isLook(eight); if(boo)return 1;
            break;
    case 5:
            temp=eight.a[1][2];
            eight.a[1][2]=eight.a[0][2];
            eight.a[0][2]=temp;
             boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[1][2];
            eight.a[1][2]=eight.a[2][2];
            eight.a[2][2]=temp;
            boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[1][2];
            eight.a[1][2]=eight.a[1][1];
            eight.a[1][1]=temp;
             boo=isLook(eight); if(boo)return 1;
            break;
    case 6:
            temp=eight.a[2][0];
            eight.a[2][0]=eight.a[1][0];
            eight.a[1][0]=temp;
             boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[2][0];
            eight.a[2][0]=eight.a[2][1];
            eight.a[2][1]=temp;
             boo=isLook(eight); if(boo)return 1;
            break;
    case 7:
            temp=eight.a[2][1];
            eight.a[2][1]=eight.a[1][1];
            eight.a[1][1]=temp;
            boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[2][1];
            eight.a[2][1]=eight.a[2][0];
            eight.a[2][0]=temp;
             boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[2][1];
            eight.a[2][1]=eight.a[2][2];
            eight.a[2][2]=temp;
             boo=isLook(eight); if(boo)return 1;
            break;
    case 8:
            temp=eight.a[2][2];
            eight.a[2][2]=eight.a[1][2];
            eight.a[1][2]=temp;
             boo=isLook(eight); if(boo)return 1;


            eight=eight_num;
            temp=eight.a[2][2];
            eight.a[2][2]=eight.a[2][1];
            eight.a[2][1]=temp;
             boo=isLook(eight); if(boo)return 1;
            break;


    }
}
bool isGoal(Eight_num &eight_num,Eight_num &eight_num1){
    int i,j;
     for( i=0;i<3;i++){
         for(j=0;j<3;j++)
            if(eight_num1.a[i][j]!=eight_num.a[i][j])
                break;
        if(j<3)
            break;
        }
    if(i<3&&j<3)
       {
         return 0;
        }
    else
        return 1;


    }
bool isLook(Eight_num &eight){
   /* deque<Eight_num>::iterator pos;
    pos=find(eight_deque.begin(),eight_deque.end(),eight);
    if(pos!=eight_deque.end()){
        eight_deque.push_back(eight);
        cout<<"insert it "<<endl;
        eight.display();
        isGoal(eight,goal_num);
    }*/


    int k=0;
    int size_1=eight_deque.size();
    for(;k<size_1;k++)
   {
    int i,j;
     for( i=0;i<3;i++){
         for(j=0;j<3;j++)
            if(eight.a[i][j]!=eight_deque[k].a[i][j])
                break;
         if(j<3)
               break;


        }
    bool boo;
    if(i<3&&j<3)
       {
           cout<<"a new and insert it"<<endl;
           eight.pro=&eight_deque[start];//  modify the pointer
           eight_deque.push_back(eight);
           boo=isGoal(eight,goal_num);
           if(boo)
              {
                  cout<<"find it  ,find it ,find  it"<<endl;
                  Eight_num *find_goal=nullptr;
                  find_goal=&eight;
                  do{
                      find_goal->display();
                      cout<<"my  father below  me ! "<<endl;
                      find_goal=find_goal->pro;


                  }while(find_goal);
              }
           return boo;
       }
       if(i==3&&j==3)
       {
       cout<<"already  exist it "<<endl;
       }


    }
    return 0;


}



猜你喜欢

转载自blog.csdn.net/zenglingcheng/article/details/80464806