/*
* @(#)EightQueens.java 1.0 2004-02-12
*
* Copyright 2004, will wang. All rights reserved.
* email farmworker@163.net farmworker@163.com.
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Vector;
public class EightQueens extends Applet {
Buttons buttons; // The buttons for calculating and drawing
queens
CheeseCanvas canvas; // The drawing area
Vector answers = new Vector();//
Answer answer=new Answer();
int x[]=new int[9];
int answerNum;
public void init() {
setLayout(new BorderLayout());
canvas = new CheeseCanvas();
add("Center", canvas);
add("South", buttons = new Buttons(this,canvas));
}
public void destroy() {
remove(buttons);
remove(canvas);
}
public void start() {
buttons.setEnabled(true);
}
public void stop() {
buttons.setEnabled(false);
}
public void processEvent(AWTEvent e) {
if (e.getID() == Event.WINDOW_DESTROY) {
System.exit(0);
}
}
public static void main(String args[]) {
Frame f = new Frame("EightQueens");
EightQueens eightQueens = new EightQueens();
eightQueens.init();
eightQueens.start();
f.add("Center", eightQueens);
f.setSize(320, 320);
f.show();
}
public String getAppletInfo() {
return "EightQueens";
}
public int doCalculate()throws Exception{
int p[] = new int[9];
answers.removeAllElements();
for (int i = 0; i <= 8; i++)
x[i] = 0;
try{
backTrack(1);
return answers.size();
}catch(Exception exp) {
return 0;
}
}
boolean place(int k)
{
int j;
for (j = 1; j
if ((Math.abs(k - j) == Math.abs(x[j] - x[k])) || (x[j] == x[k]))
return false;
return true;
}
void backTrack(int t)
{
if (t>8) {
for (int j = 1; j <= 8; j++){
answer.x[j-1]=x[j];
}
answers.add(answer);
answer=new Answer();
}
else for (int i = 1; i <= 8; i++){
x[t]=i;
if(place(t))backTrack(t+1);
}
}
public int showFirst(){
try{
answer=(Answer)answers.firstElement();
canvas.showAnswer(answer,1);
return 1;}
catch (Exception e){
return -1;
}
}
public int showPrevious(){
try{
int index=answers.indexOf(answer) ;
if( index==0) return 0;
answer=(Answer)answers.elementAt(index-1);
canvas.showAnswer(answer,index);
return 1;}
catch (Exception e){
return -1;
}
}
public int showNext(){
try{
int index=answers.indexOf(answer) ;
if( index==answers.size()-1) return 0;
answer=(Answer)answers.elementAt(index+1);
canvas.showAnswer(answer,index+2);
return 1;}
catch (Exception e){
return -1;
}
}
public int showLast(){
try{
answer=(Answer)answers.lastElement();
canvas.showAnswer(answer,answers.size());
return 1;}
catch (Exception e){
return -1;
}
}
}
class Answer{
int x[];
Answer(){
x=new int[8];
}
}
class CheeseCanvas extends Canvas {
Answer grid=null;
int index=0;
public void paint(Graphics g) {
drawPanel();
if (index>0)drawAnswer();
}
private void drawPanel(){
Graphics g=getGraphics();
int i,j;
g.setColor(Color.black);
for (i = 1; i <= 8; i++) {
for (j =1; j <= 8; j++) {
if (((i+j)&1)==1){//奇数
g.fillRect((i-1)*40,(j-1)*40,40,40);
}
}
}
g.drawRect(0, 0, 320, 320);
}
public void showAnswer(Answer gg,int index){
grid=gg;
this.index=index;
repaint();
}
private void drawAnswer(){
Graphics g=getGraphics();
int x,y;
if (grid==null) return;
g.setColor(Color.red);
g.drawString("index = " + index, 40, 350);
for (int i = 0; i
x=(i)*40-15;y=(8-grid.x[i])*40;
g.drawLine(x+20,y+28,x+50,y+28);g.drawLine(x+20,y+25,x+15,y+15);
g.drawLine(x+15,y+15,x+28,y+25);g.drawLine(x+28,y+25,x+28,
y+10);
g.drawLine(x+28,y+10,x+35,y+25);g.drawLine(x+35,y+25,x+42,y+10);
g.drawLine(x+42,y+10,x+42,y+25);g.drawLine(x+42,y+25,x+55,y+15);
g.drawLine(x+55,y+15,x+50,y+25);
try {
Thread.currentThread().sleep(300);
} catch (InterruptedException e) {
}
}
}
}
class Buttons extends Panel
implements ActionListener {
CheeseCanvas canvas;
EightQueens myApp;
Button bDoit,bFirst,bPrevious,bNext,bLast;
public Buttons(EightQueens App,CheeseCanvas myCanvas) {
this.myApp=App;
this.canvas = myCanvas;
bDoit = new Button("Do it!");
bDoit.addActionListener(this);
add(bDoit);
bFirst = new Button("first");
bFirst.addActionListener(this);
add(bFirst);
bFirst.setEnabled(false);
bPrevious = new Button("previous");
bPrevious.addActionListener(this);
add(bPrevious);
bPrevious.setEnabled(false);
bNext = new Button("next");
bNext.addActionListener(this);
add(bNext);
bNext.setEnabled(false);
bLast = new Button("last");
bLast.addActionListener(this);
add(bLast);
bLast.setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();
int i;
try {
if ("Do it!".equals(arg)) {
myApp.doCalculate();
if (myApp.showFirst()==1){
bFirst.setEnabled(false);
bPrevious.setEnabled(false);
bNext.setEnabled(true);
bLast.setEnabled(true);
}
} else if ("first".equals(arg)) {
if (myApp.showFirst()==1){
bFirst.setEnabled(false);
bPrevious.setEnabled(false);
bNext.setEnabled(true);
bLast.setEnabled(true);
}
} else if ("next".equals(arg)) {
if (myApp.showNext()==0){
bNext.setEnabled(false);
bLast.setEnabled(false);
}else{
bFirst.setEnabled(true);
bPrevious.setEnabled(true);
bNext.setEnabled(true);
bLast.setEnabled(true);
}
} else if ("previous".equals(arg)) {
if (myApp.showPrevious()==0){
bFirst.setEnabled(false);
bPrevious.setEnabled(false);
}else{
bFirst.setEnabled(true);
bPrevious.setEnabled(true);
bNext.setEnabled(true);
bLast.setEnabled(true);
}
} else if ("last".equals(arg)) {
if (myApp.showLast()==1){
bFirst.setEnabled(true);
bPrevious.setEnabled(true);
bNext.setEnabled(false);
bLast.setEnabled(false);
}
} else {
}
} catch(Exception exp) {
}
}
}
【 在 lilei0311 (POPO) 的大作中提到: 】
: 请问~:什么是回溯算法,及在java中如何实现?
: 用java如何实现八皇后问题,请高手指点~~
: 小弟新来,请多关照
: ...................