Question: C program for eight queens You are suggested to use recursive techniques to solve this problem. Noted that whether the ith queen could be placed
C program for eight queens
You are suggested to use recursive techniques to solve this problem. Noted that whether the ith queen could be placed into the current position is dependent on whether all the following i+1(th) to (N-1)th queens can find their positions. Note that in C, an array starts with index 0, so the (N-1)th queen is the last one. Here is some helper pseudocode to get you started:
#define N 8 int matrix[N][N]; void print_matrix(){ int i,j; for ( i = 0 ; i < N ; i++ ){ for( j = 0 ; j < N ; j++ ){ printf( "%d ", matrix[i][j]); } printf( " " ); } printf( " " ); } int valid( int i , int j ){ // Check the current queen in row i and column j is not in the same row, column or diagonal as another queen // If valid, return 1 else return 0 // Write your code here... } void putall( int id ){ /*id: index for the current queen*/ for ( i = start_position of id; i < 8*8 ; i++ ){ if( valid(i) ){ /*if current position is a valid one*/ chessboard[i/8][i%8] = 1; if( id == N-1 ){ /*if this is the last one to place*/ find a solution and output result } else{ calculate the start_position for next queen. putall( id + 1 ); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
