Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.awt.*; import java.util.*; public class NQueens { static ChessBoard board; public static void main (String[] argv) { solveQueens (8, 8); } static void solveQueens

image text in transcribed

import java.awt.*; import java.util.*; public class NQueens { static ChessBoard board; public static void main (String[] argv) { solveQueens (8, 8); } static void solveQueens (int numQueens, int size) { board = new ChessBoard (size); boolean solutionExists = solve (numQueens, 0); if (! solutionExists) { System.out.println ("No solution for " + numQueens + " on a " + size + " x " + size + " board"); return; } System.out.println ("Solution found for " + numQueens + " on a " + size + " x " + size + " board"); System.out.println (board); board.display(); } static boolean solve (int numQueens, int whichCol) { // Bottom-out condition 1: if (numQueens == 0) { // None to assign - done. return true; } // Bottom-out condition 2: if (whichCol >= board.size()) { // No columns left to try: done. return false; } // Try every un-forbidden spot in each row. for (int row=0; row 

import java.util.*; import java.awt.*; import javax.swing.*; public class ChessBoard { // Instance variables. int size; char[][] board; // board[i][j] == 'X' if there's a queen on it. // Constructor. public ChessBoard (int size) { // Build the board. Initially empty: board[i][j] == 'O'. this.size = size; board = new char [size][size]; for (int i=0; i 

// File: ImageTool.java // // To convert Java's Image instances to pixel arrays and back. // Author: Rahul Simha // Modified: Aug 21, 2006 // Modified: Aug 29, 2006 import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; // A JPanel to draw the image. Must override paintComponent. class ImagePanel extends JPanel { Image image; public void paintComponent (Graphics g) { if (image != null) { g.drawImage (image, 0,0, this); } else { g.drawString ("No image", 50,50); } } } // end of ImagePanel class // The main class - ImageTool. This has four useful methods: // (1) readImageFile() - to read an Image in GIF/JPG/PNG format // from a file in the current directory. Returns an instance // of java.awt.Image after it has been completely read from file. // (2) imageToPixels() - to convert a Java Image instance into a 2D // array. Each 2D pixel is itself four int's: one each for red, // blue, green and alpha (transparency). // (3) pixelsToImage() - to go the other way. // (4) showImage() - to bring up a frame with the given image. public class ImageTool extends JFrame { static int locationX = 0; static int locationY = 0; public void showImage (Image image) { showImage (image, "No title"); } public void showImage (Image image, String title) { // Make a frame and set parameters. JFrame f = new JFrame (); f.setSize (400, 300); f.setTitle (title); locationX += 20; locationY += 20; f.setLocation (locationX, locationY); // Add an instance of the image-drawing panel. //** Change to include image in ImagePanel's constructor. ImagePanel drawPanel = new ImagePanel (); drawPanel.image = image; Container cPane = f.getContentPane(); cPane.add (drawPanel, BorderLayout.CENTER); // Display. //** Add a windowclosing listener. f.setVisible (true); } public int[][][] imageToPixels (Image image) { // Java's peculiar way of extracting pixels is to give them // back as a one-dimensional array from which we will construct // our version. int numRows = image.getHeight (this); int numCols = image.getWidth (this); int[] oneDPixels = new int[numRows*numCols]; // This will place the pixels in oneDPixels[]. Each int in // oneDPixels has 4 bytes containing the 4 pieces we need. PixelGrabber grabber = new PixelGrabber(image, 0, 0, numCols, numRows, oneDPixels ,0 ,numCols); try { grabber.grabPixels (0); } catch (InterruptedException e) { System.out.println (e); } // Now we make our array. int[][][] pixels = new int[numRows][numCols][4]; for(int row = 0; row > 24) & 0xFF; // Alpha pixels[row][col][1] = (aRow[col] >> 16) & 0xFF; // Red pixels[row][col][2] = (aRow[col] >> 8) & 0xFF; // Green pixels[row][col][3] = (aRow[col]) & 0xFF; // Blue } } return pixels; } public Image pixelsToImage (int[][][] pixels) { int numRows = pixels.length; int numCols = pixels[0].length; int[] oneDPixels = new int [numRows * numCols]; int index = 0; for(int row=0; row  

image text in transcribed

In-Class Exercise 14: Can 3 queens be placed on a 3x.3 board I ? Can 3 queens be placed on a 4x4 board? Download and modify NQucens.java and ChessBoard java to find out. You will also need ImageTool java and queen.jpg In-Class Exercise 14: Can 3 queens be placed on a 3x.3 board I ? Can 3 queens be placed on a 4x4 board? Download and modify NQucens.java and ChessBoard java to find out. You will also need ImageTool java and queen.jpg

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions