Question

In: Computer Science

Complete the 8 queens 1 dimensional array program with backtracking in c+++. (don't use go to...

Complete the 8 queens 1 dimensional array program with backtracking in c+++. (don't use go to statement ) and please describe all the code statement and show the code in complier and also which can be copied. thank you very much

Solutions

Expert Solution

Hello i am providing the code fir 8 queens 1 dimensional array program with backtracking in c++ without any go to statement

please go through it once and if have any doubt please ask in comment i will be very happy to assist you

And please upvote if this helps you

 /**  Problem: EightQueens Problem
 *     The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so 
 *     that no two queens attack each other. Thus, a solution requires that no two queens share the same row, 
 *         column, or diagonal. The eight queens puzzle is an example of the more general n-queens problem of placing
 *         n queens on an n×n chessboard, where solutions exist for all natural numbers n with the exception of 2 and 3.
 *  
 *      
 *  Input Format: 
 *      No Input is required, just run the program and it will display output arrangements of 8-queens.
 *
 *  OutPut Format:
 *      This program will out put all possible 8 queen arrangements on Chessboard, where no two queens attack  
 *      each other. i.e. this solution will output arrangements such that no two queens share the same row, column, 
 *      or diagonal. In the Output, 1 will represent queen and 0 non-queen.
 *
 * @Compiler version on which Program is Last Run before uploading to Github: Dev-C++ 5.4.1, Date: 1st July, 2013  
 * @author Gurpreet Singh
 */
// 8 queens using 1D array....with backtracking removing all gotos
    
    #include <iostream>
    #include<cstdlib>
    #include <cmath>
    using namespace std;

    bool ok(int q[], int col){
        for(int i=0;i<col;i++){ 
         if((q[i] == q[col]) || (abs(q[col] - q[i]) == (col - i))) 
         {   return false;
         }
       }
       return true;
    }//boolean function ends here
    
   void backtrack(int &col){
        col--;
        if(col==-1) exit(1);
       }//backtrack function ends here

   void print(int q[])
    {  static int count =0;
       count++;
       int i,j,board[8][8]={0};
       cout<<"#"<<count<<endl;
       for( i=0;i<8;i++)
       {
         board[q[i]][i]=1;           
       }
       
       for(i=0;i<8;i++)                 //final print board
        { for(j=0;j<8;j++)
             cout<<board[i][j]<<" ";
             
           cout<<endl;
        }
       
    }// Print function ends here

    int main()
    {
      int q[8]; q[0]=0;
      int c=1;
      // from_backtrack keeps track if we need to reset the row to the
      // top of the current colum or not.

      bool from_backtrack=false;
      // The outer loop keep looking for solutions
      // The program terminates from function backtrack
      // when we are forced to backtack into column -1
      while(1){
        while(c<8){ //this loop goes across columns
        // if we just returned from backtrack, use current value of row
        // otherwise get ready to start at the top of this column
         if(!from_backtrack) // we did not just return from backtrack
          q[c]=-1;   //check here
            from_backtrack=false;
              while(q[c]<8){ // place queen in this column
                q[c]++;
                  // if row=8, there is no valid square in this column
                 // so backtrack and continue the loop in the previous column
                 
                
                    while(q[c]==8)
                     { backtrack(c);
                       q[c]++;
                                  
                     }
                //if this position is ok, place the queen
                // and move on (break) to the next column,
                // otherwise keep looking in this column
                 
                  if(ok(q, c))
                     break;
                                   
              }// while q[c]<8
          c++; // placed ok, move to the next column
     }//while(c<8)
     // one complete solution found, print it.
     print(q); // board completed, print it out
     backtrack(c);
     from_backtrack=true;
    }//while(1)
   
}//main method

OUTPUT :

NOTE : There is total 92 boards in output can't add all here but adding some here thank you

Board 1-6 Board 87-92


Related Solutions

Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is...
Complete the "dumb" 8 queens program that Use the 1 dimensional array representation. C++ This is the solution to the  question. i want this program to written in different WAY. nothing fancy. #include<cmath> #include<fstream> #include<iostream> using namespace std; bool ok(int b[][8]){ int rQueens=0, dQueens=0; for(int row=0; row<8; row++){ for(int column=0; column<8; column++){ //Rows test if(b[row][column]==1) rQueens++; if(rQueens>1) return false; //Diagonals test    for(int j=1; ((column-j)>=0)&&((row-j)>=0); j++){ if(b[row-j][column-j]==1&&b[row][column]==1) return false; } for(int k=1; ((column-k)>=0)&&((row+k)<8); k++){ if(b[row+k][column-k]==1&&b[row][column]==1) return false; } } rQueens=0; }...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
Backtracking Write a program in c++ that moves an asterisk ‘*’ in a grid of 10x10...
Backtracking Write a program in c++ that moves an asterisk ‘*’ in a grid of 10x10 cells. Your program should start with * on position (2,3) i.e. 3rd column of 2nd row. After printing the grid and ‘*’, your program will ask user if he wants to move the ‘*’ Up, Down, Left or Right. If user presses ‘U’, ‘D’, ‘L’ or ‘R’, your program will move the ‘*’ one position accordingly. If user presses ‘Q’, your program should terminate....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT