1150 简单魔方

最初的实现代码,但提交不成功,经过认真思考,我目前想到的问题即是:本实现方法使用了深度优先算法,如果步长允许的话,会循环一圈,如AAAA后在做其他预算。

#include "stdlib.h"

#include "iostream"

#define N 10

using namespace std;

struct Node{

       int a1;

       int a2;

       int a3;

       int a4;

       int a5;

       int a6;

       int a7;

       int a8;       

};

struct Node myNode[N+1];

struct Node EndState;

char resultSteps[N];

int steps;//表示输入的总步数 

bool stopFlag;

bool isEqual(struct Node a,struct Node b)

{

     return a.a1==b.a1&&a.a2==b.a2&&a.a3==b.a3&&a.a4==b.a4&&a.a5==b.a5&&a.a6==b.a6&&a.a7==b.a7&&a.a8==b.a8;

}

bool search(int flag,int stepInfo);

int main()

{



    myNode[0].a1=1;

    myNode[0].a2=2;

    myNode[0].a3=3;

    myNode[0].a4=4;

    myNode[0].a5=8;

    myNode[0].a6=7;

    myNode[0].a7=6;

    myNode[0].a8=5;

    //myNode

    while(cin>>steps&&steps!=-1)

    {

        cin>>EndState.a1>>EndState.a2>>EndState.a3>>EndState.a4>>EndState.a5>>EndState.a6>>EndState.a7>>EndState.a8;

		stopFlag=true;

        //先判断是否和最初步数一样

        if(isEqual(EndState,myNode[0]))

        {

              cout<<"0"<<endl;

              continue;

        } 

        bool result=false;

        resultSteps[0]='A';

        //cout<<"***"<<1<<"A"<<"***"<<endl;

        result=search(1,1);

        if(!result)

        {

             resultSteps[0]='B';

             //cout<<"***"<<1<<"B"<<"***"<<endl;

             result=search(2,1);

             

        }

        if(!result)

        {

             resultSteps[0]='C';

             //cout<<"***"<<1<<"C"<<"***"<<endl;

             result=search(3,1); 

        }

        if(!result)

        {

		 		   cout<<"-1"<<endl;

		}

    }

    //system("pause");

    return 0;

}

bool search(int flag,int stepInfo)

{

     if(!stopFlag)

     {

          return true;

     }

     if(stepInfo>=steps)

     {

		   return false;

	 }

     if(flag==1)

     {

          myNode[stepInfo].a1=myNode[stepInfo-1].a5;

          myNode[stepInfo].a2=myNode[stepInfo-1].a6;

          myNode[stepInfo].a3=myNode[stepInfo-1].a7;

          myNode[stepInfo].a4=myNode[stepInfo-1].a8;

          myNode[stepInfo].a5=myNode[stepInfo-1].a1;

          myNode[stepInfo].a6=myNode[stepInfo-1].a2;

          myNode[stepInfo].a7=myNode[stepInfo-1].a3;

          myNode[stepInfo].a8=myNode[stepInfo-1].a4;

          

          if(!isEqual(myNode[stepInfo],EndState))

          {

                bool result=false;

                resultSteps[stepInfo]='A';

                //cout<<"***"<<stepInfo+1<<"A"<<"***"<<"     ";

                result=search(1,stepInfo+1);

                if(!result)

                {

                     resultSteps[stepInfo]='B';

                     //cout<<"***"<<stepInfo+1<<"B"<<"***"<<"     ";

                     result=search(2,stepInfo+1);

                }

                if(!result)

                {

                     resultSteps[stepInfo]='C';

                     //cout<<"***"<<stepInfo+1<<"C"<<"***"<<"     ";

                     result=search(3,stepInfo+1); 

                     

                }

          }

          else

          {

              stopFlag=false;

              cout<<stepInfo<<" ";

              for(int i=0;i<stepInfo;i++)

              {

                      cout<<resultSteps[i];

              }

              cout<<endl;

              return true;

          }

     }

     if(flag==2)

     {

          myNode[stepInfo].a1=myNode[stepInfo-1].a4;

          myNode[stepInfo].a2=myNode[stepInfo-1].a1;

          myNode[stepInfo].a3=myNode[stepInfo-1].a2;

          myNode[stepInfo].a4=myNode[stepInfo-1].a3;

          myNode[stepInfo].a5=myNode[stepInfo-1].a8;

          myNode[stepInfo].a6=myNode[stepInfo-1].a5;

          myNode[stepInfo].a7=myNode[stepInfo-1].a6;

          myNode[stepInfo].a8=myNode[stepInfo-1].a7;

          if(!isEqual(myNode[stepInfo],EndState))

          {

                bool result=false;

                resultSteps[stepInfo]='A';

                //cout<<"***"<<stepInfo+1<<"A"<<"***"<<"     ";

                result=search(1,stepInfo+1);

                if(!result)

                {

                     resultSteps[stepInfo]='B';

                     //cout<<"***"<<stepInfo+1<<"B"<<"***"<<"     ";

                     result=search(2,stepInfo+1);



                }

                if(!result)

                {

                     resultSteps[stepInfo]='C';

                     //cout<<"***"<<stepInfo+1<<"C"<<"***"<<"     ";

                     result=search(3,stepInfo+1); 

                     

                }

          }

          else

          {

              stopFlag=false;

              cout<<stepInfo<<" ";

              for(int i=0;i<stepInfo;i++)

              {

                      cout<<resultSteps[i];

              }

              cout<<endl;

              return true;

          }

     }

     if(flag==3)

     {

          myNode[stepInfo].a1=myNode[stepInfo-1].a1;

          myNode[stepInfo].a2=myNode[stepInfo-1].a6;

          myNode[stepInfo].a3=myNode[stepInfo-1].a2;

          myNode[stepInfo].a4=myNode[stepInfo-1].a4;

          myNode[stepInfo].a5=myNode[stepInfo-1].a5;

          myNode[stepInfo].a6=myNode[stepInfo-1].a7;

          myNode[stepInfo].a7=myNode[stepInfo-1].a3;

          myNode[stepInfo].a8=myNode[stepInfo-1].a8;

          if(!isEqual(myNode[stepInfo],EndState))

          {

                bool result=false;

                resultSteps[stepInfo]='A';

                //cout<<"***"<<stepInfo+1<<"A"<<"***"<<"     ";

                result=search(1,stepInfo+1);

                if(!result)

                {

                     resultSteps[stepInfo]='B';

                     //cout<<"***"<<stepInfo+1<<"B"<<"***"<<"     ";

                     result=search(2,stepInfo+1);



                }

                if(!result)

                {

                     resultSteps[stepInfo]='C';

                     //cout<<"***"<<stepInfo+1<<"C"<<"***"<<"     ";

                     result=search(3,stepInfo+1); 

                     

                }

          }

          else

          {

              stopFlag=false;

              cout<<stepInfo<<" ";

              for(int i=0;i<stepInfo;i++)

              {

                      cout<<resultSteps[i];

              }

              cout<<endl;

              return true;

          }

     }

     return false;

}

  

你可能感兴趣的:(简单)