#include <bits/stdc++.h>
using namespace std;
struct Node
{
int board[9];
//int father[9];
int g;//depth
int h;
int f;
int index;
struct Node * next;
};
int g=0;
//2->3
int flag=0;
struct Node * open=NULL;
struct Node * curr_open=open;
struct Node * close=NULL;
struct Node * last_close=close;
void close_addition(struct Node * addtoclose,int goal[]){
cout<<"Inside close addition "<<endl;
int c=0;
last_close=last_close->next;
last_close=addtoclose;
g++;
for(int i=0;i<9;i++){
if(last_close->board[i]==goal[i]){
c++;
}
if(c==9){
flag=1;
}
if(flag==1){
cout<<"g value"<<g<<endl;
}
}
}
void deletefromopen(priority_queue <int, vector<int>, greater<int> > minf,struct Node*open){
cout<<"Inside Delete from open"<<endl;
struct Node * temp=((struct Node*)(malloc(sizeof(struct Node))));
struct Node * addtoclose=((struct Node*)(malloc(sizeof(struct Node))));
struct Node * temp_prev=((struct Node*)(malloc(sizeof(struct Node))));
int goal[9]={1,2,3,8,0,4,7,6,5};
if(open->f==minf.top()){
temp=open;
open=open->next;
addtoclose=temp;
close_addition(addtoclose,goal);//close**************
free(temp);
}
else{
while(temp->f!=minf.top()){
temp_prev=temp;
temp=temp->next;
}
temp_prev->next=temp->next;
addtoclose=temp;
close_addition(addtoclose,goal);
free(temp);
}
}
//insert
void inserttoopen(int f, int g,int h,int demo[]){
cout<<"Inside INSERT TO OPEN "<<endl;
struct Node* new_open=((struct Node*)(malloc(sizeof(struct Node))));
for(int i=0;i<9;i++){
new_open->board[i]=demo[i];
}
new_open->f=f;
new_open->g=g;
new_open->h=h;
curr_open=curr_open->next;
curr_open=new_open;
priority_queue <int, vector<int>, greater<int> > minf;
minf.push(curr_open->f);
deletefromopen(minf,open);
}
int h1vals(struct Node * last_close,int h,int goal[],int x,int y,int g){
cout<<"Inside h1"<<endl;
int demo[9]={0};
for(int i=0;i<9;i++)
{
demo[i]=last_close->board[i];
}
swap(demo[x],demo[y]);
for(int i=0;i<9;i++)
{ if(goal[i]!=demo[i])
{
h++;//h+g
}
}
int f=h+g;
inserttoopen(f,g,h,demo);
for(int i=0;i<9;i++)
{
demo[i]=last_close->board[i];
}
}
//cout<<"Calculating MISPLACED TILES"<<endl;
//cout<<"MISPLACED Tiles possible board position is: "<<endl;
// b++;
//cout<<demo[i]<<" ";
//if(b==3){
// cout<<endl;
//b=0;
//}
// }
//cout<<endl<<"h1 Misplaced value is "<<co<<endl;
int main(){
struct Node * open=NULL;
struct Node * curr_open=open;
struct Node * close=NULL;
struct Node * last_close=close;
int goal[9]={1,2,3,8,0,4,7,6,5};int zero_pos=0;
cout<<"Enter the input state"<<endl;
for(int i=0;i<9;i++)
{
cin>>close->board[i];
}
cout<<"flag"<<flag<<endl;
while(flag!=1)
{
for(int i=0;i<9;i++)
{
if(last_close->board[i]==0)
{
zero_pos=i;
}
}
if(zero_pos==0){
h1vals(last_close,0,goal,0,1,g);
h1vals(last_close,0,goal,0,3,g);
}
if(zero_pos==1)
{
h1vals(last_close,0,goal,1,0,g);
h1vals(last_close,0,goal,1,2,g);
h1vals(last_close,0,goal,1,4,g);
}
if(zero_pos==2)
{
h1vals(last_close,0,goal,2,1,g);
h1vals(last_close,0,goal,2,5,g);
}
if(zero_pos==3)
{
h1vals(last_close,0,goal,3,0,g);
h1vals(last_close,0,goal,3,4,g);
h1vals(last_close,0,goal,3,6,g);
}
if(zero_pos==4)
{
h1vals(last_close ,0,goal,4,5,g);
h1vals(last_close, 0,goal,4,3,g);
h1vals(last_close,0,goal,4,1,g);
h1vals(last_close,0,goal,4,7,g);
}
if(zero_pos==5)
{
h1vals(last_close,0,goal,5,4,g);
h1vals(last_close,0,goal,5,2,g);
h1vals(last_close,0,goal,5,8,g);
}
if(zero_pos==6)
{
h1vals(last_close,0,goal,6,7,g);
h1vals(last_close,0,goal,6,3,g);
}
if(zero_pos==7)
{
h1vals(last_close,0,goal,7,6,g);
h1vals(last_close,0,goal,7,8,g);
h1vals(last_close,0,goal,7,4,g);
}
if(zero_pos==8)
{
h1vals(last_close,0,goal,8,7,g);
h1vals(last_close,0,goal,8,5,g);
}
}
return 0;
}
It's not even printing the flag value in the main.
This is the output: .
I think it should be because of the declaration of variables in the global and not getting referenced in the main. Please help me sort out the same.
User contributions licensed under CC BY-SA 3.0