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(); } }
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(); } }
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education