Question

In: Computer Science

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;
}

return true;
};

void print(int b[][8], int count){
cout << "SOLUTION " << count << ": " << endl;
   for(int row=0; row<8; row++){
       for(int column=0; column<8; column++){
           cout << b[row][column] << " ";
       }
cout << endl;
   }
   cout << endl;
};

int main(){
/*
int board[8][8]={1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0};
int board2[8][8]={0,0,1,0,0,0,0,0,
0,0,0,0,0,1,0,0,
0,0,0,1,0,0,0,0,
0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,
0,0,0,0,1,0,0,0,
0,0,0,0,0,0,1,0,
1,0,0,0,0,0,0,0};
int board3[8][8]={1,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,
0,0,1,0,0,0,0,0,
0,0,0,1,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0};
cout<<":"<<ok(board)<<endl; //false, 0
cout<<":"<<ok(board2)<<endl; //true, 1
cout<<":"<<ok(board3)<<endl; //false, 0
*/

int board[8][8]={0};
int count = 0;
for(int i0 =0; i0 <8; i0 ++)
for(int i1 =0; i1 <8; i1 ++)
for(int i2 =0; i2 <8; i2 ++)
for(int i3 =0; i3 <8; i3 ++)
for(int i4 =0; i4 <8; i4 ++)
for(int i5 =0; i5 <8; i5 ++)
for(int i6 =0; i6 <8; i6 ++)
for(int i7 =0; i7 <8; i7 ++){

// use the indices of the loops to set a configuration in array board
// if this configuration is conflict-free, print the count and the board
board[i0][0]=1;
board[i1][1]=1;
board[i2][2]=1;
board[i3][3]=1;
board[i4][4]=1;
board[i5][5]=1;
board[i6][6]=1;
board[i7][7]=1;

if(ok(board)) print(board, ++count);

// reset the board for the next configuration
board[i0][0]=0;
board[i1][1]=0;
board[i2][2]=0;
board[i3][3]=0;
board[i4][4]=0;
board[i5][5]=0;
board[i6][6]=0;
board[i7][7]=0;
}
return 0;
}

PLEASE DONT COPY AND PASTE THE SIMILAR ONCES ON THE CHEGG . AS I HAVE BEEN AWARE OF.

Solutions

Expert Solution

 #include <cmath> #include <iostream> using namespace std;                       bool ok(int board[][8]){                for(int c = 7; c > 0; c--){ int r = 0; while(board[r][c] != 1 ){ r++; } for(int i = 1; i <= c; i++){ if(board[r][c-i] == 1) return false; else if (board[r-i][c-i] == 1) return false; else if (board[r+i][c-i] == 1) return false; } } return true; }    void print(int board[][8], int count){ cout << count << endl; for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ cout << board[i][j]; } cout << endl; } } int main (){ int board[8][8]; for ( int i = 0; i < 8; i++ ) for ( int j = 0; j < 8; j++ ) board[i][j] = 0; int count = 0; for(int i0 = 0; i0 < 8; i0++) for(int i1=0; i1 < 8; i1++) for(int i2 = 0; i2 < 8; i2++) for(int i3 = 0; i3 < 8; i3++) for(int i4 = 0; i4 < 8; i4++) for(int i5 = 0; i5 < 8; i5++) for(int i6 = 0; i6 < 8; i6++) for(int i7 = 0; i7 < 8; i7++){ board[i0][0]=1; board[i1][1]=1; board[i2][2]=1; board[i3][3]=1; board[i4][4]=1; board[i5][5]=1; board[i6][6]=1; board[i7][7]=1; if(ok(board))print(board, ++count); board[i0][0]=0; board[i1][1]=0; board[i2][2]=0; board[i3][3]=0; board[i4][4]=0; board[i5][5]=0; board[i6][6]=0; board[i7][7]=0; } return 0; }

this is the code which i wote in my code editor I did this using arrays concept and solved it the easiest possible way. you may run it your device and see this code is succesfully running i tried my best to solve your promblem hope this helps u. Do put a thumbs up to my answer if it helps you. Thank you very much.


Related Solutions

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
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and...
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and 30 columns. Row represents sections of a course and column represents the students, value inside each position of the array is the final exam grade for each students. Fill the array with random numbers between 40 and 100. Calculate the total, average, maximum, minimum for each section. Please do it simple. code
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++.
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT