Question

In: Computer Science

write project Queens problems / c language

write project Queens problems / c language

Solutions

Expert Solution

//C program

#include<stdio.h>
#include<stdbool.h>
# define n 4
bool issafe_row(int a[][n],int row,int col){
    int i;
    for(i=0;i<col;i++){
        if(a[row][i]==1)return false;
   }
   return true;
}
bool issafeupdiagonal(int a[][n],int row,int col){
    while(row>0&&col>0){
        if(a[row-1][col-1]==1)return false;
        row--;
        col--;
   }
   return true;
}

bool issafedown_diagonal(int a[][n],int row,int col){
    while(row<n&&col>0){
        if(a[row+1][col-1]==1)return false;
        row++;
        col--;
   }
   return true;
}




bool issafe(int a[][n],int row,int col){
    return issafe_row(a,row,col)&&issafedown_diagonal(a,row,col)&&issafeupdiagonal(a,row,col);
}

bool queen(int a[][n],int col){
   if(col>=n)return true;
   int i;
   for(i=0;i<n;i++){
       if(issafe(a,i,col)==true){
           a[i][col]=1;
           if(queen(a,col+1)==true)return true;
           a[i][col]=0;
       }
   }
   return false;
  
}

void print(int a[][n]){
   int i,j;
   for(i=0;i<n;i++){
       for( j=0;j<n;j++)
       printf("%d ",a[i][j]);
       printf("\n");
   }
}

int main(){
   int arr[n][n];
   int i,j;
   for( i=0;i<n;i++){
       for(j=0;j<n;j++)
       arr[i][j]=0;
   }
   if(queen(arr,0)==true){
       print(arr);
   }
}

//sample output


Related Solutions

Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
Write the following in C language for Arduino: Write a program that turns on the LED...
Write the following in C language for Arduino: Write a program that turns on the LED at 25%, 50%, 75%, 100%, and then 0% brightness with a one second delay in between each change. Remember you are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine the values for...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
write a general example of polling in C language with comments
write a general example of polling in C language with comments
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT