and this is my solution for the 8 queens puzzle

This is a discussion on and this is my solution for the 8 queens puzzle within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h>//for the abs /*=========================================== checks wether there is a manace betwean queens and returns values of ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    9

    and this is my solution for the 8 queens puzzle

    Code:
    #include <stdio.h>
    #include <stdlib.h>//for the abs
    /*===========================================
    checks wether there is a manace betwean 
    queens and returns values of zero as manace
    and one as no manace
    ===========================================*/
    int manace(int board[],int size){
    	int row;
    	
    	for(row=0;row<size;row++)
    	{
    		if(board[row]==board[size])
    			return 0;//same row
    		if(abs(board[row]-board[size])==abs(row-size))
    			return 0;//same diagonal
    	}//for row
    	return 1;//no manace
    }//manace
     /*======================================================
     composed of 8 for loops one within the other the passage
     betwean each loop is conditioned by the manace function
    =======================================================*/
    void manage(int line[], int num){
    	int counter=0;//counts solution
    	
    	for(line[0]=0;line[0]<=7;line[0]++)
    	{
    		if (manace(line,0)==0) continue;
    		for(line[1]=0;line[1]<=7;line[1]++) 
    		{
    			if (manace(line,1)==0) continue;
    			for(line[2]=0;line[2]<=7;line[2]++)	
    			{
    				if (manace(line,2)==0) continue;
    				for(line[3]=0;line[3]<=7;line[3]++) 
    				{
    					if (manace(line,3)==0) continue;
    					for(line[4]=0;line[4]<=7;line[4]++) 
    					{
    						if (manace(line,4)==0) continue;
    						for(line[5]=0;line[5]<=7;line[5]++) 
    						{
    							if (manace(line,5)==0) continue;
    							for(line[6]=0;line[6]<=7;line[6]++) 
    							{
    								if (manace(line,6)==0) continue;
    								for(line[7]=0;line[7]<=7;line[7]++) 
    								{
    									if (manace(line,7)==0) continue;
    									else 
    									{
    										counter++;//the increasment of the counter
    										if (counter==num) return;
    										else break;
    									}//else
    								}//for 7
    							}//for 6
    						}//for 5
    					}//for 4
    				}//for 3	
    			}//for 2
    		}//for 1
    	}//for 0
    }//manage
    
     /*===================================================================
     prints the solution which is a one dimantional array as a chess board
    ===================================================================*/
    void print(int arr[]){
    	int i,j;//indexes which represents location on the chess bord
    	
    	printf("---------------------------------\n");
    	for(i=0;i<=7;i++)
    	{
    		printf("|");
    		for(j=0;j<=7;j++)
    			if(j==arr[i])
    				printf(" * |");
    			else printf("   |");
    			putchar('\n');
    			printf("---------------------------------\n");			
    	}
    }
    
    /*========================================================================
    main function recives an input from user and operates the other functions
    =======================================================================*/
    void main() {
    	int line[8]={0},solution;//zeroes the entire array as a start platform
    	
    	printf("Enter the number of the solution [1..92]\n");
    	scanf("%d",&solution);
    	while(solution<1||solution>92)
    	{
    		printf("Wrong number, insert an other one\n");
    		scanf("%d",&solution);
    	}//if solution
    
    	manage(line,solution);//a call for the manage function.
    	putchar('\n');
    	print(line);//a call for the pint fuction.
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Well I didn't read the actual solution. However as far as stylistic/standard concerns:
    Your many many levels of nested fors' is incredibly ugly, turn it into a nice function call. Then you might be able to make it N Queens.
    main returns an int, so int main. Also, prototype main, not declare it: int main(void). Since main returns an int you need to return one at the end of the function.
    Your scanf doesn't work well if they enter a value which is not an integer.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21