VB icon

a Recursion : 8 Queens On a Chess Board

Email
Submitted on: 1/2/2015 7:12:00 PM
By: Alon Ben David (from psc cd)  
Level: Intermediate
User Rating: By 7 Users
Compatibility: C, C++ (general), Borland C++
Views: 5908
 
     the program find a place to put queens on a chess board in any size ,using recursion.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: a Recursion : 8 Queens On a Chess Board
// Description:the program find a place to put queens on a chess board in any size ,using recursion.
// By: Alon Ben David (from psc cd)
//
// Side Effects:a great example of recursions .
//**************************************

/* this program made by alon ben david */
#include <stdio.h>
#define N 8
int Chess(char Arr[N][N] , int row);
int check(char Arr[N][N],int row,int line);
//double count;
int main()
{
char chess[N][N]={0};
	
Chess(chess,0);/* The call to the function*/
{
int i,y;
		
for(i=0;i<N;++i)/*prints the result*/
{
printf("\n\t\t\t");
for(y=0;y<N;++y)
{
if(chess[i][y]==0)
printf("x ");
else
printf("%c ",chess[i][y]);	
}
}
}
printf("\n");
}
int Chess(char Arr[N][N] , int row)
{
int line=0;
	
//printf("%f\n",count++);
if(row==N)
return 1;
		
while(line < N)
{
if(check(Arr,row,line)) /*check the row*/
{
Arr[row][line]='Q'; /*puts a queen on the board*/
if(Chess(Arr,row+1))/*the recursion*/
return 1;
Arr[row][line]=0;/*clears the last change if*/
}/*returned 0 from the recursion*/
line++;
}
return 0;
}
int check(char Arr[N][N],int row,int line)
{/*check just the left size of the board*/
int r,l;
	
r=row;
l=line;
while(r >= 0 && l >= 0)
{
if(Arr[r][l]=='Q')
return 0;
--r;
--l;
}
	
l=line;
r=row;
while(l < N && r >= 0)
{
if(Arr[r][l]=='Q')
return 0;
++l;
--r;
}
	
l=line;
r=row;
while(r >= 0)
{
if(Arr[r][l]=='Q')
return 0;
--r;	
}
return 1;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.