Skip to content

Instantly share code, notes, and snippets.

@tomrockdsouza
Created June 2, 2017 11:27
Show Gist options
  • Save tomrockdsouza/ae9ade2f6675e5d5c9ed70d671faa89e to your computer and use it in GitHub Desktop.
Save tomrockdsouza/ae9ade2f6675e5d5c9ed70d671faa89e to your computer and use it in GitHub Desktop.
C Implementation of 8puzzle problem using Heuristic Function
/**
* Implementation of 8puzzle problem using Heuristic Function
* Needed matrix can be changed by altering variable array[9]
* 1 2 3
* 8 0 4
* 7 6 5
*
* Input at the start Example
* Enter values:2 8 3 1 6 4 7 0 5
*
* Email: [email protected]
* @author Tomrock D'souza, St. Francis Institute Of Technology, University of Mumbai, 2017
* @copyright GNU General Public License v3.0
* No reproduction in whole or part without maintaining this copyright notice
* and imposing this condition on any subsequent users.
*/
#include<stdio.h>
#include<conio.h>
//Prints the 3x3 Array
void printr(int *ar)
{
int n1;
for(n1=0; n1<3; n1++)
printf("%d %d %d\n",*(ar+n1*3+0),*(ar+n1*3+1),*(ar+n1*3+2));
}
//Checks THe Smaller interger value
int smaller(int c,int d)
{
if(c<d) {
return 1;
}
else {
return 0;
}
}
//Compares Array 1 with array 2 and returns misplaced boxes
int cmpr(int *arr1,int *arr2)
{ int a,b=0;
for(a=0; a<9; a++)
{ if(*(arr1+a)==*(arr2+a)&&*(arr2+a)!=0)
{
b++;
}
}
return 7-b;
}
//Copies one array to another array
void cpyr(int *arr1,int *arr2)
{ int a;
for(a=0; a<9; a++)
{
*(arr2+a)=*(arr1+a);
}
}
//Swaps two numbers By Reference
void swap(int *i, int *j)
{ int k;
k=*i;
*i=*j;
*j=k;
}
int main()
{
//opointer is the position value of the empty block
int opointer=9,roundher=10,moves=0,tempmove=0,temp2=-1;
int array[9]= {1,2,3,8,0,4,7,6,5},temparr[9],herar[9],i,j,k;
//TRAV array stores the possible moves of the block ( 9 Means the block is unmovable )
int trav[9][4]= {{9,1,3,9},{9,2,4,0},{9,9,5,1},{0,4,6,9},{1,5,7,3},{2,9,8,4},{3,7,9,9},{4,8,9,6},{5,9,9,7}};
printf("Enter values:");
for(i=0;i<9;i++)
{
scanf("%d",&temparr[i]);
if(temparr[i]==0){
opointer=i;
}
}
printf("Intial Array\n");
printr(&temparr[0]);
printf("not matching elements:%d\n",cmpr(&array[0],&temparr[0]));
getch();
//The Input Seassions is over here.
//While condition is set to check if there are no non matching elements
while(cmpr(&array[0],&temparr[0])!=-1)
{
printf("---------------------------\n");
for(i=0; i<4; i++) {
//Try all 4 possible areas to move with respect to the 0 pointer
if(trav[opointer][i]!=9&&temp2!=i)
{
if(i==0) {
printf("Up ");
}
else if(i==1) {
printf("Right ",i);
}
else if(i==2) {
printf("Down ",i);
}
else {
printf("Left ",i);
}
//Copy array
cpyr(&temparr[0],&herar[0]);
//Swap the 0pointer Element
swap(&herar[trav[opointer][i]],&herar[opointer]);
printf("Value: %d\n",cmpr(&array[0],&herar[0])+1);
k=cmpr(&array[0],&herar[0]);
if(smaller(k,roundher))
{ roundher=k;
tempmove=i;
}
}
}
//Excuting Move After Deciding
printf("\nMoving ");
if(tempmove==0) {
printf("Up \n");
temp2=2;
}
else if(tempmove==1) {
printf("Right\n",i);
temp2=3;
}
else if(tempmove==2) {
printf("Down\n",i);
temp2=0;
}
else {
printf("Left\n",i);
temp2=1;
}
swap(&temparr[trav[opointer][tempmove]],&temparr[opointer]);
opointer=trav[opointer][tempmove];
moves++;
printr(&temparr[0]);
printf("moves: %d\n",moves);
roundher=10;
if(getch()=='y') {
printf("User Aborted\n");
break;
}
}
printf("----------------------------\nSolution Found. \nEnd");
getch();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment