📄 queensolver.java
字号:
//// Eight Queens puzzle written in Java// Written by Tim Budd, January 1996// revised for 1.3 event model July 2001//import java.awt.*;import java.awt.event.*;import javax.swing.*;class Queen { // data fields private int row; private int column; private Queen neighbor; // constructor Queen (int c, Queen n) { // initialize data fields row = 1; column = c; neighbor = n; } public boolean findSolution() { if (neighbor != null && neighbor.canAttach(row, column)) if (! advance()) return false; return true; } public boolean advance() { if (row < 8) { row++; return findSolution(); } if (neighbor != null) { if (! neighbor.advance()) return false; // if (! neighbor.findSolution()) // return false; } else return false; row = 1; return findSolution(); } private boolean canAttach(int testRow, int testColumn) { int columnDifference = testColumn - column; if ((row == testRow) || (row + columnDifference == testRow) || (row - columnDifference == testRow)) return true; if (neighbor != null) return neighbor.canAttach(testRow, testColumn); return false; } public void paint (Graphics g) { // first draw neighbor if (neighbor != null) neighbor.paint(g); // then draw ourself // x, y is upper left corner 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.drawOval(x+20, y+20, 10, 10); } public void foo(Queen arg, Graphics g) { if (arg.row == 3) g.setColor(Color.red); }}public class QueenSolver extends JFrame { public static void main(String [ ] args) { QueenSolver world = new QueenSolver(); world.setTitle("8 queens"); world.setSize(600, 500); // world.show(); world.setVisible(true); world.work(); } private Queen lastQueen = null; public QueenSolver() { addMouseListener(new MouseKeeper()); addWindowListener(new CloseQuit()); } void work(){ for (int i = 1; i <= 8; i++) { lastQueen = new Queen(i, lastQueen); lastQueen.findSolution(); } } public void paint(Graphics g) { super.paint(g); // draw board for (int i = 0; i <= 8; i++) { g.drawLine(50 * i + 10, 40, 50*i + 10, 440); g.drawLine(10, 50 * i + 40, 410, 50*i + 40); } g.drawString("Click Mouse for Next Solution", 20, 470); // draw queens lastQueen.paint(g); } private class CloseQuit extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } private class MouseKeeper extends MouseAdapter { public void mousePressed (MouseEvent e) { lastQueen.advance(); repaint(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -