2016-05-10 4 views
-1

私はスタックを使用して8パズルを解決しようとしています。このコードは、 プログラムがそれを実行できるかどうかを知るためのものです。 。 プログラムは最初の文字列を繰り返し実行しますが、2つの新しい文字列をプッシュした後にジョブを実行します。それだけでそれらをポップするが、機能を続行doesnt。誰も私のプログラムがスタックにプッシュを停止する理由を知っています

私は何をすることができますか?

#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
#include <cstdlib> 
#include "stack.cpp" 

using namespace std; 

bool checkFound(string meta,string aVer); 
string converArretoString(STACK <string> stack, string ac); 
void crearPosibilidad(STACK <string> open,string ac); 
void checkRow(STACK <string> stack, int actual[3][3],int row,int col); 
void checkCol(STACK <string> stack, int actual[3][3],int row,int col); 

int main(int argc, char** argv) 
{ 
    string meta=""; 
    string actual="120345678"; 
    STACK <string> state; 
    STACK <string> ustate; 
    STACK <string> steps; 

    state.push(actual); 
    while (!state.empty()) 
    { 
     actual.clear(); 
     actual=state.pop(); 
     // cout<<actual<<endl; 
     if(checkFound(meta,actual)) 
     { 
      cout<<actual; 
      cout<< "lo encontramos"; 
      return 0; 
     } 

      //cout<<actual<<endl; 
      ustate.push(actual); 
      crearPosibilidad(state,actual); 

    } 
return 0; 
} 
//checa si se encontro la solucion 
bool checkFound(string meta,string aVer) 
{ 
    if(aVer==meta) 
    { 
     return true; 
    } 
    return false; 
} 
//creara los posibles escenarios 
void crearPosibilidad(STACK <string> state,string ac) 
{ 
    int act [3][3]; 
    int i=-1; 
    int col=0; 
    int ren=0; 
    string a; 
    for (int r = 0; r<3; r++) 
    { 
     for(int c=0;c<3;c++) 
     { 
      i++; 
      a=ac[i]; 
      act[r][c]=stoi(a); 
      if (act[r][c]==0) 
      { 
       cout<<r<<endl; 
       cout<<c<<endl; 
       ren=r; 
       col=c; 
      } 
     } 
    } 
    checkCol(state,act,ren,col); 
    checkRow(state,act,ren,col); 
} 

string converArretoString(int actual[3][3]) 
{ 
    string temp; 
    ostringstream conver; 
    for(int r=0;r<3;r++) 
    { 
     for(int c=0;c<3;c++) 
     { 
      conver<<actual[r][c]; 
     } 
    } 
    temp=conver.str(); 
    cout<<temp<<endl; 
    return temp; 
} 
void checkRow(STACK <string> stack, int actual[3][3],int row,int col) 
{ 
    int temp; 
    string tems; 
    if(row==0) 
    { 
     //mover abajo 
     temp=actual[row+1][col]; 
     actual[row][col]=temp; 
     actual[row+1][col]=0; 
     tems=converArretoString(actual); 
     //cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      // cout<<tems<<endl; 
      stack.push(tems); 
     } 
     //original 
     temp=actual[row][col]; 
     actual[row+1][col]=temp; 
     actual[row][col]=0; 
    } 
    if(row==1) 
    { //moverabajo 
     temp=actual[row+1][col]; 
     actual[row][col]=temp; 
     actual[row][col]=0; 
     tems=converArretoString(actual); 
     //cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      // cout<<tems<<endl; 
      stack.push(tems); 
     } 
     //mover arriba 
     temp=actual[row][col]; 
     actual[row][col]=actual[row-1][col]; 
     actual[row+1][col]=temp; 
     actual[row-1][col]=0; 
     tems=converArretoString(actual); 
     //cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      // cout<<tems<<endl; 
      stack.push(tems); 
     } 
     //original 
     temp=actual[row][col]; 
     actual[row-1][col]=temp; 
     actual[row][col]=0; 

    } 

    if(row==2) 
    { 
     //mover arriba 
     temp=actual[row-1][col]; 
     actual[row][col]=temp; 
     actual[row-1][col]=0; 
     tems=converArretoString(actual); 
     //cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      // cout<<tems<<endl; 
      stack.push(tems); 
     } 
     //original 
     temp=actual[row][col]; 
     actual[row-1][col]=temp; 
     actual[row][col]=0; 
    } 


} 

void checkCol(STACK<string> stack, int actual[3][3],int row, int col) 
{ 
    int temp; 
    string tems; 
    if(col==0) 
    { 

     //mover derecha 
     temp=actual[row][col+1]; 
     actual[row][col]=temp; 
     actual[row][col+1]=0; 
      //put the generated child on the queue 
     tems=converArretoString(actual); 
     //cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      //cout<<tems<<endl; 
      stack.push(tems); 
     } 
      //return to the orginal 
     temp=actual[row][col]; 
     actual[row][col+1]=temp; 
     actual[row][col]=0; 

    }//if 
    if(col==1) 
    { 
     //move right 
     temp=actual[row][col+1]; 
     actual[row][col]=temp; 
     actual[row][col+1]=0; 
     tems=converArretoString(actual); 
     //cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      //cout<<tems<<endl; 
      stack.push(tems); 
     } 

     //mover izquierda 
     temp=actual[row][col]; 
     actual[row][col]=actual[row][col-1]; 
     actual[row][col+1]=temp; 
     actual[row][col-1]=0; 

     tems=converArretoString(actual); 
     //cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      //cout<<tems<<endl; 
      stack.push(tems); 
     } 
     //orginal 
     temp=actual[row][col]; 
     actual[row-1][col]=temp; 
     actual[row][col]=0; 
    } 

    if(col==2) 
    { 

     //mover izquierda 
     temp=actual[row][col-1]; 
     actual[row][col]=temp; 
     actual[row][col-1]=0; 
     tems=converArretoString(actual); 
     // cout<<tems<<endl; 
     if(!stack.search(tems)) 
     { 
      //cout<<tems<<endl; 
      stack.push(tems); 
     } 
      //orginal 
     temp=actual[row][col]; 
     actual[row][col-1]=temp; 
     actual[row][col]=0; 

    } 

} 
+1

あなたが使用している 'STACK'機能の実装を知らなければ、それがどのように動作するかを知ることはできません。しかし、 'stack.push()'が動作することがわかっていると仮定すると、 '!stack.search(tems)'がfalseの場合、 'if(row)'と 'if col) '型のチェックが必要になります。 – Jameson

+0

プログラムのデバッグを試してください –

答えて

0

ご参考までにSTACKを渡すのはどうですか?あなたの実際のコード、converArretoString()crearPosibilidad()checkRow()checkCol()

は値によってSTACK<std::string>変数を受けています。そのため、関数によって破棄されたスタックのコピーを受け取ります。

checkRow()およびcheckCol()に、押され、値

stack.push(tems); 

が押さ値が関数によって出射失われます。

私は

string converArretoString(STACK <string> & stack, const string & ac); 
void crearPosibilidad(STACK <string> & open,string ac); 
void checkRow(STACK <string> & stack, int actual[3][3],int row,int col); 
void checkCol(STACK <string> & stack, int actual[3][3],int row,int col); 

このように関数のシグネチャを変更することを提案(あなたの問題との相関なし)その他の提案:const参照して、変数を渡す、可能な場合。例:checkFoud()はコピーで2 std::stringを受信します。関数は値を変更しないので、const参照で文字列を渡すことができます。何かのように

bool checkFound (const string & meta, const string & aVer) 
{ return aVer == meta; } 

p.s .:私の悪い英語のために申し訳ありません。

+0

うまくいきました。私はまだ、どのように動作しているのか理解していません。でもありがとう。 p.s心配しないでください私の心配も最悪です – Abe

関連する問題