Tuesday, January 28, 2020

8puzzle using bfs in c program

#include<stdio.h>
#include<stdlib.h>
struct node{
int arr[10][10];
struct node* link;
int i,j;

};
int a[10][10];
struct node* q[200];


int r=0,c=0,gotit=0;
void operations(int k,int m,int n);
void display(int n);
void insert(int n);
int check(int n);
main()
{
int n=3;
insert(n);
display(n);

}

void insert(int n)
{
int i,j;

struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter 8 puzzle matrix intial state");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&temp->arr[i][j]);
}
}
printf("Enter 8 puzzle matrix goal state");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
temp->link=NULL;
// here finding the zero position and zero index is storing in that particular node
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(temp->arr[i][j]==0)
{

temp->i = i;
temp->j = j;
}
}
}
int k,m;
q[r] = temp;
while(1)
{
if( check(n)==-4)
{
break;
}


k = q[r]->i;
m = q[r]->j;

 //down
if(++k<n && m<n && (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;
//up
if(--k<n && m<n&& (k>=0 && m>=0))
{
      operations(k,m,n);
  }

k = q[r]->i;
m = q[r]->j;
//left
if(k<n && --m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

k = q[r]->i;
m = q[r]->j;
// right
if(k<n && ++m<n&& (k>=0 && m>=0))
{
operations(k,m,n);
}

r++;
if( check(n)==-4)
{
break;
}


}
}
// operations
void operations(int k,int m,int n)
{

int o,p;
struct node* t;
t = (struct node*)malloc(sizeof(struct node));
int i,j;
// copying the parent node into child node
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
t->arr[i][j] = q[r]->arr[i][j];

}
}
// finding the zero positin
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{
o = i;
p=j;
}
}
}
// swaping the zero based up on left,right,up,down operations
int swap = t->arr[o][p];
t->arr[o][p] = t->arr[k][m];
t->arr[k][m] = swap;
//finding the zero position of child node
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(t->arr[i][j]==0)
{                                                                                                                                                 
  t->i = i;
  t->j = j;
}
}
}
t->link = q[r];
c++;
q[c] = t;

}
int check(int n)
{
int i,j,k,i1,i3,c1=0,c2,g2=0;
int count=0;

  // here it is removing the node if duplicate matrix is present

for(i=0;i<c;i++)
{


for(i3=i+1;i3<c;i3++)
{
count = 0 ;
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{

if(q[i]->arr[j][k]==q[i3]->arr[j][k])
{
count++;

}
}

}
if(count==9)
{
      for(i1=i3;i1<c;i1++)
      {
       q[i1] = q[i1+1];
       
  }
  c--;
count = 0;
}

count = 0;
}

}
// here it is checking whether the goal state is finding or not
c2=0;
for(i1=0;i1<c;i1++)
{


for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{

if(q[i1]->arr[j][k]==a[j][k])
{
c2++;

}
}

}
if(c2==9)
{
gotit = i1;
g2 = -4;
break;
}
else{
c2=0;
}

}


return g2;

}
//Displaying
void display(int n)
{
printf("printing in reverse order\n");
struct node* t;

t=q[gotit];
int i,j;
while(t!=NULL)
{
for( i=0;i<n;i++)
{
for( j=0;j<n;j++)
{
printf("%d\t",t->arr[i][j]);
}
printf("\n");
}
printf("\n");
t = t->link;
}

}

No comments:

Post a Comment

Manhattan Distance Heuristic

#include<stdio.h> #include<stdlib.h> struct node{  int arr[10][10];  int i,j;  int md; }; int a[10][10]; struct node*...