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...
Assignment (C language, not C ++) Start a new project to store grade information for 5...
Assignment (C language, not C ++) Start a new project to store grade information for 5 students using structures. Declare a structure called student_t that contains : First name Last name Student ID Number Percentage grade Letter grade Create the following functions: getStudentInfo(void)- Declares a single student object - Uses printf()/scanf() to get keyboard input for Name, SID, and Percentage (not Letter). - Returns a single student structure calcStudentGrade(student_t *st_ptr)- Takes the pointer to a student (to avoid making a...
C Mini Project is a mini application that could be developed using C language that involves...
C Mini Project is a mini application that could be developed using C language that involves the concepts of arrays, functions, read and write data techniques. Based on your creativity, you are required to plan, design and develop a mini application for an organisation. You may choose to from the list below or propose your own mini application: 1. Customer Billing System 2. Employee Record System 3. Contact Management System 4. Appointment Management System 5. Attendance Record System Your responsibility...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT