Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question
TRANSLATE TO PYTHON
import java.util.*;

public class queen {

finalprivateint [][] map = newint[8][8];
finalprivateint [][] testMap = newint[8][8];
privateintheuristic = 0;
privateintqueenLocs = 0;
privateintrestarts = 0;
privateintmoves = 0;
privateintneighbors = 8;


publicqueen( ){ //initializes the map
for(inti = 0; i < 8; i++){
for(intj = 0; j < 8; j++){
map[i][j] = 0;
}
}
}


publicvoidrandomizeMap( ){ //randomizes the map
Randomrand = newRandom( );
intnum;

while(queenLocs < 8){
for(inti = 0; i < 8; i++){
map[rand.nextInt(7)][i] = 1;
queenLocs++;
}
}
heuristic = heuristic(map);
}

//***************************Heuristic****************************//

publicbooleanfindRowEx(int [][] test, inta){ //determines row conflicts
booleanexFound = false;
intcount = 0;

for(inti = 0; i < 8; i++){
if(test[i][a] == 1){
count++;
}
}
if(count > 1){
exFound = true;
}
returnexFound;
}

publicbooleanfindColEx(int [][] test, intj){ //determines column conflicts
booleanexFound = false;
intcount = 0;
for(inti = 0; i < 8; i++){
if(test[j][i] == 1){
count++;
}
}
if(count > 1){
exFound = true;
}
returnexFound;
}

publicbooleanfindDiaEx(int [][] test, inta, intb){//determines diagonal conflicts
booleandiaFound = false;

for(inti = 1; i < 8; i++){
if(diaFound){
break;
}

if((a+i < 8)&&(b+i < 8)){
if(test[a+i][b+i] == 1){
diaFound = true;
}
}
if((a-i >= 0)&&(b-i >= 0)){
if(test[a-i][b-i] == 1){
diaFound = true;
}
}
if((a+i < 8)&&(b-i >= 0)){
if(test[a+i][b-i] == 1){
diaFound = true;
}
}
if((a-i >= 0)&&(b+i < 8)){
if(test[a-i][b+i] == 1){
diaFound = true;
}
}
}
returndiaFound;
}

publicintheuristic(int [][] test){//Counts the number of queens in conflict
intcount = 0;
booleanrowEx;
booleancolEx;
booleandiaEx;

for(inti = 0; i < 8; i++){
for(intj= 0; j < 8; j++){
if(test[i][j] == 1){
rowEx = findRowEx(test, j);
colEx = findColEx(test, i);
diaEx = findDiaEx(test, i, j);

if(rowEx || colEx || diaEx){
count++;
}
}
}
}
returncount;
}

//***********************Move Queen***********************//
publicvoidmoveQueen( ){ //moves a queen and determines whether to continue to a new state or restart or to summarize solution
int[][] hArray = newint[8][8];
intcolCount;
intminCol;
intminRow;
intprevColQueen = 0;
while(true){
colCount = 0;

for(inti = 0; i < 8; i++){
System.arraycopy(map[i], 0, testMap[i], 0, 8);
}
while(colCount < 8){
for(inti = 0; i < 8;i++){
testMap[i][colCount] = 0;
}
for(inti = 0; i < 8; i++){
if(map[i][colCount] == 1){
prevColQueen = i;
}
testMap[i][colCount] = 1;
hArray[i][colCount] = heuristic(testMap);
testMap[i][colCount] = 0;
}
testMap[prevColQueen][colCount] = 1;
colCount++;
}

if(determineRestart(hArray)){
queenLocs = 0;
for(inti = 0; i < 8; i++){
for(intj = 0; j < 8; j++){
map[i][j] = 0;
}
}
randomizeMap( );
System.out.println("RESTART");
restarts++;
}

minCol = findMinCol(hArray);
minRow = findMinRow(hArray);

for(inti = 0; i < 8; i++){
map[i][minCol] = 0;
}

map[minRow][minCol] = 1;
moves++;
heuristic = heuristic(map);

if(heuristic(map) == 0){
System.out.println("\nCurrent State");
for(inti = 0; i < 8; i++){
for(intj = 0; j < 8; j++){
System.out.print(map[i][j] + " ");
}
System.out.print("\n");
}
System.out.println("Solution Found!");
System.out.println("State changes: " + moves);
System.out.println("Restarts: " + restarts);
break;
}

System.out.println("\n");
System.out.println("Current h: " + heuristic);
System.out.println("Current State");
for(inti = 0; i < 8; i++){
for(intj = 0; j < 8; j++){
System.out.print(map[i][j] + " ");
}
System.out.print("\n");
}
System.out.println("Neighbors found with lower h: " + neighbors);
System.out.println("Setting new current State");
}
}

publicintfindMinCol(int[][] test){ //finds column of minimum neighbor state
intminCol = 8;
intminVal = 8;
intcount = 0;

for(inti = 0; i < 8; i++){
for(intj = 0; j < 8; j++){
if(test[i][j] < minVal){
minVal = test[i][j];
minCol = j;
}
if(test[i][j] < heuristic){
count++;
}
}
}
neighbors = count;
returnminCol;
}

publicintfindMinRow(int[][] test){ //finds row of minimum neighbor state
intminRow = 8;
intminVal = 8;

for(inti = 0; i < 8; i++){
for(intj = 0; j < 8; j++){
if(test[i][j] < minVal){
minVal = test[i][j];
minRow = i;
}
}
}
returnminRow;
}

publicbooleandetermineRestart(int [][] test){// determines whether restart is necessary
intminVal = 8;
booleanrestart = false;

for(inti = 0; i < 8; i++){
for(intj = 0; j < 8; j++){
if(test[i][j] < minVal){
minVal = test[i][j];
}
}
}
if(neighbors == 0){
restart = true;
}
returnrestart;
}
/************************* Main **********************/
publicstaticvoidmain(String[] args) {// creates object, creates initial random map, then initiates state change
queenone = newqueen();
one.randomizeMap();
one.moveQueen();
}
}
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education