c++ - 8-Puzzle Solver unexpected behavior -


i working on 8-puzzle solver -using best-first search+ hamming distance(tiles out of place) heuristic- required project.

i first defined stat struct in separated cpp file looks in header file :

#ifndef stat_h_included #define stat_h_included #include <iostream>  struct stat {     int board[3][3];     int depth;     int empty[2];     int evaluation;     int operator- (stat) const; //overloading operator- return "hamming distance"     void operator= (stat);     bool operator== (stat) const;     bool operator< (stat) const; };  //used compare class set container  class comparor  //class compare between stats(default bigger) {     bool bigger;  public:     comparor(const bool& smaller=1)     {         bigger=smaller?0:1;     }     bool operator() (const stat& lhs, const stat& rhs) const     {         if (bigger) return (lhs<rhs?0:1);         else return (lhs<rhs);     } }; std::istream& operator>> (std::istream&,stat&); //to input 9-cells 1  std::ostream& operator<< (std::ostream&,stat&); //to output 9-cells 1 #endif // stat_h_included 

next main.cpp file :

#include <vector> #include <set> #include <algorithm> #include <fstream> #include "stat.h"  using namespace std;  //global variables stat start,goal,temp; int steps=0; set<stat,comparor> open; //used stats arranged -by heuristic value- , unique vector<stat> closed;  //forward declaration int solver(); inline int generate_move(char);  int main() {     ifstream cin("input.txt");      cin>>start>>goal;     start.depth=0;     start.evaluation=start-goal;     open.insert(start);     solver();     cout<<temp<<endl;     cout<<endl<<"setps reach goal = "<<steps<<endl;     return 0; }  int solver() {   set<stat,comparor>::iterator it=open.begin();   temp=*it;   if(temp==goal)     return steps;   cout<<temp<<endl;   closed.push_back(temp);   open.erase(it);    start=temp;   if(temp.empty[0]<2) //up direction     generate_move('u');   if(temp.empty[0]>0) //down direction     generate_move('d');   if(temp.empty[1]<2) //right direction     generate_move('r');   if(temp.empty[1]>0) //left direction     generate_move('l');    steps++;   solver(); }  inline int generate_move(char direction) {   int index,inverse,row,coloum;   int e0=temp.empty[0],e1=temp.empty[1];    if(direction == 'u'){index=1;inverse=0;}   else if(direction == 'd'){index=-1;inverse=0;}   else if(direction == 'r'){index=0;inverse=1;}   else if(direction == 'l'){index=0;inverse=-1;}    row=e0+index;   coloum=e1+inverse;    swap(temp.board[e0][e1],temp.board[row][coloum]); //swapping empty cell adjacent cell   if(find(closed.begin(),closed.end(),temp)!=closed.end())   {       temp=start;       return 0;   }   //changing place of empty cell new place   temp.empty[0]=row;   temp.empty[1]=coloum;    temp.depth++; //increasing depth of stat    //setting heuristic value of stat   temp.evaluation=goal-temp;   temp.evaluation+=temp.depth;    open.insert(temp);   temp=start;   return 0; } 

now when run program sample input :

2 8 3 1 6 4 7 0 5  1 2 3 8 0 4 7 6 5 

it solves puzzle , good.but every other input tried open set became empty before reaching goal despite fact there is solution puzzles.

edit: tried see stats entered open set , didn't used input below sample , got :

stats entered open :

2 8 3    2 8 3    2 0 3    0 2 3    1 2 3     1 2 3 1 6 4    1 0 4    1 8 4    1 8 4    0 8 4     8 0 4 7 0 5    7 6 5    7 6 5    7 6 5    7 6 5     7 6 5 

which steps needed solve puzzle.

stats not found in closed , didn't enter open:

2 8 3    2 8 3     2 8 3    1 2 3 1 6 4    1 4 0     0 1 4    7 8 4 0 7 5    7 6 5     7 6 5    0 6 5 

so , open set says above stats exists in open , refuse input them there no such stats in open , heuristic values of states different on open(4,4,5,5,5,5) others(6,6,5,7).

can please tell me why open refuse enter stats if exist. in advance.


Comments

Popular posts from this blog

Java sticky instances of class com.mysql.jdbc.Field aggregating -