// // Code for the 8queen problem using the heuristic // Astar method and using four different ways to // decide the basis for index calculation // // Sameer Singh // #include #include #include //These are the global constant storing the dimension //of the square matrix in N (2 to 8) and the output //filename in filename[] const int N=8; const char filename[]="astar.out"; FILE *fp=fopen(filename,"w"); //This is a string copy function different from the //inbuilt one since it ignores the NULL character void strcpy(unsigned char *a, unsigned char *b) { for(int i=0;iN)&&(j>N)&&(i<0)&&(j<0)) return -1; j=N-j-1; return int(map[i]/pow(2,j))%2; } //This function follows similiar logic as get() to assign a value //to any cell of the board void set(unsigned char i,unsigned char j,unsigned char v) { if((i=0)&&(j>=0)) { if(v==0) if(get(i,j)==1) map[i]-=int(pow(2,N-j-1)); if(v==1) { if(get(i,j)==0) { map[i]+=int(pow(2,N-j-1)); } } } } //This function assigns the index of the noe, and returns it //also. It has 4 methods. They are explained below. Each of //them can be seperately selected by commenting as required float calc_index() { // Code for heuristic index calculation // 1. By Maximising the number of queens // This is a simple method in which the node with most // queens placed is preferred. Thus it does not revert // to an earlier state unless no space is left on board // Finished 8x8 in 25943 steps // index=queens; //Uncomment this statement // 2. By Maximising the number of Attacking squares // In this method, the preference is given to moves // which lead to most number of squares getting // attacked. // Finished 8x8 in 867 steps /* index=0.0; for(int i=0;imap,map); start->queens=queens; strcpy(start->x,x); strcpy(start->y,y); start->calc_index(); start->next=NULL; } else { node a; strcpy(a.map,map); a.queens=queens; float i=a.calc_index(); node *temp=start; if(i>=start->index) { node *temp2=new node; strcpy(temp2->map,map); temp2->queens=queens; strcpy(temp2->x,x); strcpy(temp2->y,y); temp2->calc_index(); temp2->next=start; start=temp2; } else { while((temp->next!=NULL)&&(temp->next->index>=i)) { temp=temp->next; } if(temp->next==NULL) { node *temp2=new node; temp->next=temp2; strcpy(temp2->map,map); temp2->queens=queens; strcpy(temp2->x,x); strcpy(temp2->y,y); temp2->calc_index(); temp2->next=NULL; } else { node *temp2=new node; temp2->next=temp->next; temp->next=temp2; strcpy(temp2->map,map); temp2->queens=queens; strcpy(temp2->x,x); strcpy(temp2->y,y); temp2->calc_index(); } } } } //Function to delete the topmost node void del() { if(start!=NULL) { node *temp=start->next; delete start; start=temp; } } }; //This function displays the details of a node alongwith //a grahical representation of the map with queens placed void disp(node t) { printf("Queens : %d\n",t.queens); for(int i=0;iqueens;i++) fprintf(fp,"(%d, %d) ",astar.start->x[i],astar.start->y[i]); fprintf(fp,"\n"); } } if(flag==-1) printf("No Solution\n"); if(flag==1) printf("Solution in %D steps!!!\n",steps); while(astar.start!=NULL) astar.del(); printf("\n\n --Sameer Singh\n"); fclose(fp); getch(); return 0; } // // // UPDATE // // // // This function takes the top of the list, stores it in // a variable and deletes it. Then it checks the node for // success and failure, returning with the approporiate // value. If it is none of them, then it adds all the // possible nodes, generated by placing another queen in // non-attacking squares, to the list. Then it returns 0 int list::update() { //Failure if(start==NULL) return -1; //Store the node node t,t2; t.queens=start->queens; strcpy(t.map,start->map); strcpy(t.x,start->x); strcpy(t.y,start->y); t.calc_index(); //Another copy t2.queens=start->queens; strcpy(t2.map,start->map); strcpy(t2.x,start->x); strcpy(t2.y,start->y); t2.calc_index(); //Delete the node del(); //Check for empty int flag=1; for(int i=0;i