⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 一个比较完整的八皇后Java求解问题
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package eightqueens;import java.awt.*;import java.awt.event.*;import javax.swing.*;class Queen {    private int row;    private int column;    private Queen neighbor;    Queen(int c, Queen n) {        row = 1;        column = c;        neighbor = n;    }    /*    public boolean findSolution() {    while (neighbor != null && neighbor.canAttack(row, column)) {    // If attacking exists, try to advance      if (!advance()) {    // If cannot advance    return false;    }    // If advance succeeded, check attacking again    }    return true;    }    public boolean advance() {    if (row < Main.QUEEN_COUNT) {    row++;    // try to find solution again    return findSolution();    }    // Back trace    if (neighbor != null) {    if (!neighbor.advance()) {    return false;    }    if (!neighbor.findSolution()) {    return false;    }    // Restart from row 1    row = 1;    return findSolution();    } else {    // Back to the first row    return false;    }    }     */    public boolean findSolution() {        while (neighbor != null && neighbor.canAttack(this.row, this.column)) {            // If attacking exists, try to advance              if (!advance()) {                // If cannot advance                return false;            }        // If advance succeeded, check attacking again        }        // No attacking now!        return true;    }    public boolean advance() {        if (this.row < Main.QUEEN_COUNT) {            this.row++;            return true;        }        if (neighbor == null) {            // This is the first queen.            return false;        }        // Back trace        if (!neighbor.advance()) {            return false;        }                if (!neighbor.findSolution()) {            return false;        }        // Restart from row 1        this.row = 1;        return true;    }    private boolean canAttack(int testRow, int testColumn) {        int columnDifference = testColumn - column;        if (row == testRow ||                row + columnDifference == testRow ||                row - columnDifference == testRow) {            return true;        }        if (neighbor != null) {            return neighbor.canAttack(testRow, testColumn);        }        return false;    }    void paint(Graphics g) {        if (neighbor != null) {            neighbor.paint(g);        }        int x = (row - 1) * 50 + 10;        int y = (column - 1) * 50 + 40;        g.drawLine(x + 5, y + 45, x + 45, y + 45);        g.drawLine(x + 5, y + 45, x + 5, y + 5);        g.drawLine(x + 45, y + 45, x + 45, y + 5);        g.drawLine(x + 5, y + 35, x + 45, y + 35);        g.drawLine(x + 5, y + 5, x + 15, y + 20);        g.drawLine(x + 15, y + 20, x + 25, y + 5);        g.drawLine(x + 25, y + 5, x + 35, y + 20);        g.drawLine(x + 35, y + 20, x + 45, y + 5);        g.drawLine(x + 20, y + 20, x + 10, y + 10);    }}/** * * @author qingxl */public class Main extends JFrame {    /**     * @param args the command line arguments     */    public static void main(String[] args) {        Main world = new Main();        world.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        world.setVisible(true);    }    public Main() {        setTitle(QUEEN_COUNT + " Queens");        setSize(600, 500);        boolean found = false;        for (int i = 1; i <= QUEEN_COUNT; i++) {            lastQueen = new Queen(i, lastQueen);            found = lastQueen.findSolution();            if (!found) {                break;            }        }        displayResult(found);        addMouseListener(new MouseKeeper());    }    public void paint(Graphics g) {        super.paint(g);        for (int i = 0; i <= QUEEN_COUNT; i++) {            g.drawLine(50 * i + 10, 40, 50 * i + 10, 40 + QUEEN_COUNT * 50);            g.drawLine(10, 50 * i + 40, 10 + QUEEN_COUNT * 50, 50 * i + 40);        }        g.drawString("Click mouse for the next solution", 20, 70 + QUEEN_COUNT * 50);        lastQueen.paint(g);    }    private void displayResult(boolean found) {        if (found) {            // Now, find a solution!            JOptionPane.showMessageDialog(null, "Found a solution!");            System.out.println("Found a solution!");        } else {            JOptionPane.showMessageDialog(null, "No solution!");            System.out.println("No solution!");        }    }    private class MouseKeeper extends MouseAdapter {        public void mousePressed(MouseEvent e) {            boolean found = lastQueen.advance() && lastQueen.findSolution();            repaint();            displayResult(found);        }    }    private Queen lastQueen = null;    public static final int QUEEN_COUNT = 4;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -