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

📄 q8.java

📁 是一个解决八皇后问题的JavaApplet小程序,可以演示
💻 JAVA
字号:
/*
 * @(#)Q8.java 1.0 03/05/09
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_2\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

import java.awt.*;
import java.applet.*;


public class Q8 extends Applet implements Runnable {
	
	static public int delay = 200;
	Graphics g;
	private Thread Q8_thread;
	boolean thread_run = false;
	private Scrollbar delaySb;
	private Button switchButton, lookButton, pauseButton;
	private List lookList = new List(7, false);
	int begin_x=60, begin_y=30;
	int x[] = new int[8];
	int sum;
	int sheet[][] = new int[9][9];
	boolean a[] = new boolean[15];
	boolean b[] = new boolean[15];
	boolean c[] = new boolean[15];
	boolean flag_run = false, flag_pause = false, 
			flag_look = false;
	
	public void init() {
		g = getGraphics();
		switchButton = new Button("开始");
		lookButton = new Button("观察");
		pauseButton = new Button("暂停");
		add(switchButton);
		add(pauseButton);
		add(lookButton);
		delaySb = new Scrollbar (Scrollbar.VERTICAL, 25, 0, 0,50 );
		add(delaySb);
		add(lookList);
	}

	public boolean action(Event e, Object o) {
		if (o.equals("开始")) {
			if (! thread_run || ! Q8_thread.isAlive()) {
				Q8_thread = new Thread(this);
				Q8_thread.start();
				thread_run = true;
			}
			for (int i=0; i<15; i++) {
				a[i] = true;
				b[i] = true;
				c[i] = true;
			}
			sum = 0;
			lookList.clear();
			if (flag_look) {
				for (int i=0; i<8; i++) 
					for (int j=0; j<8; j++)
						rubber_q(i+1,j+1);
				flag_look = false;
			}				
			g.setColor(Color.black);
			flag_run = true;
			lookButton.enable(false); 
			pauseButton.enable(true);
			switchButton.setLabel("停止");
		}
		if (o.equals("观察")) {
			if (flag_look) {
				for (int i=0; i<8; i++) 
					for (int j=0; j<8; j++)
						rubber_q(i+1,j+1);
			}
			String str = " " + lookList.getSelectedItem();
			for (int i=0; i<8; i++) {
				int k = 3*(i+1);
				print_q(i+1, Integer.parseInt(str.substring(k-1, k)));
			}
			flag_look = true;
		}
		if (o.equals("暂停")) {
			flag_pause = true;
			//flag_run = false;
			switchButton.enable(false);
			pauseButton.setLabel("继续");
		}
		if (o.equals("继续")) {
			flag_pause = false;
			//flag_run = true;
			switchButton.enable(true);
			pauseButton.setLabel("暂停");
		}
		if (o.equals("停止")) {
			flag_run = false;
			lookButton.enable(true); 
			pauseButton.enable(false);
			switchButton.setLabel("开始");
		}		
		return true;
	}	
	
	public boolean handleEvent(Event e) {
		delay = delaySb.getValue()*20;
		return super.handleEvent(e);
	}
	
	public void stop() {
		Q8_thread.stop();
		Q8_thread = null;
	}
	
	public void run() {
		q8_run(0);
		
		flag_run = false;
		lookButton.enable(true); 
		pauseButton.enable(false);
		switchButton.setLabel("开始");
	}
	
	
	
	public void paint(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0,0,500,300);
		g.setColor(Color.black);
		switchButton.reshape(388,36,60,20);
		pauseButton.reshape(388,72,60,20);
		lookButton.reshape(396,252,60,20);
		delaySb.reshape(350,30,15,72);
		lookList.reshape(336,110,127,136);
		g.drawString("∧",336,50);
		g.drawString("快",336,72);
		g.drawString("∧",336,92);
		for (int i=0; i<=8; i++) {
			g.drawLine(i*30+begin_x, begin_y, i*30+begin_x, 240+begin_y);
			g.drawLine(begin_x, i*30+begin_y, 240+begin_x, i*30+begin_y);
			if (i > 0) {
				g.drawString(String.valueOf(i), i*30-16+begin_x, begin_y-3);
				g.drawString(String.valueOf(i), begin_x-10, i*30-12+begin_y);
			}
		}
	}
	
	public void print_q(int x, int y) {
		g.setColor(Color.black);
		g.setFont(new Font("TimesToman", Font.BOLD, 27));
		g.drawString("Q",x*30-25+begin_x,y*30-5+begin_y);
	}
	
	public void rubber_q(int x, int y) {
		g.setColor(Color.white);
		g.setFont(new Font("TimesToman", Font.BOLD, 27));
		g.drawString("Q",x*30-25+begin_x,y*30-5+begin_y);
	}
	
	public void print_take(int x, int y) {
		g.setColor(new Color(128, 128, 128));
		g.fillRoundRect(x*30-16+begin_x,y*30-16+begin_y,4,4,2,2);
	}
	
	public void rubber_take(int x, int y) {
		g.setColor(Color.white);
		g.fillRoundRect(x*30-16+begin_x,y*30-16+begin_y,4,4,2,2);
	}
	
	//画dot
	public void print_dot(int i){
		for (int k=x[i]+1; k<=8; k++) {
			print_take(i+1, k);
			sheet[i+1][k] += 1;
		}
		for (int k=i+2; k<=8; k++) {
			print_take(k,x[i]);
			sheet[k][x[i]] += 1;
		}
		for(int k=1; k<=8; k++) {
			if ((k+i+1 > 8) || (k+x[i] > 8))
				break;
			print_take(k+i+1, k+x[i]);
			sheet[k+i+1][k+x[i]] += 1;
		}
		for(int k=1; k<=8; k++) {
			if ((k+i+1 > 8) || (x[i]-k < 1))
				break;
			print_take(k+i+1, x[i]-k);
			sheet[k+i+1][x[i]-k] += 1;
		}
	}
	//除dot
	public void rubber_dot(int i){
		for (int k=x[i]+1; k<=8; k++) {
			sheet[i+1][k] -= 1;
			if (sheet[i+1][k] == 0)
				rubber_take(i+1, k);
		}
		for (int k=i+2; k<=8; k++) {
			sheet[k][x[i]] -= 1;
			if (sheet[k][x[i]] == 0)
				rubber_take(k,x[i]);
		}
		for(int k=1; k<=8; k++) {
			if ((k+i+1 > 8) || (k+x[i] > 8))
				break;
			sheet[k+i+1][k+x[i]] -= 1;
			if (sheet[k+i+1][k+x[i]] == 0)
				rubber_take(k+i+1, k+x[i]);
		}
		for(int k=1; k<=8; k++) {
			if ((k+i+1 > 8) || (x[i]-k < 1))
				break;
			sheet[k+i+1][x[i]-k] -= 1;
			if (sheet[k+i+1][x[i]-k] == 0)
				rubber_take(k+i+1, x[i]-k);
		}
	}
	
	public void q8_run(int i) {
		for ( int j=0; j<8; j++) {
			while (flag_pause) {}
			if (!flag_run) break;
			//位置i,j是否为空
			if (a[j] & b[i+j] & c[i-j+7]){
				x[i] = j+1;
				print_q(i+1,x[i]);
								
				a[j] = false;
				b[i+j] = false;
				c[i-j+7] = false;
				//画dot
				print_dot(i);
				
				if (flag_run) delay();
				if (i<7) {
					q8_run(i+1);					
				}
				else{
					String str="";
					for (int k=0; k<8; k++) {
						 str += (" "+String.valueOf(x[k])+" ");
					}
					lookList.addItem(str);
					sum += 1;
					
				}
				rubber_q(i+1,x[i]);
				
				a[j] = true;
				b[i+j] = true;
				c[i-j+7] = true;
				//除dot
				rubber_dot(i);
	
				if (flag_run) delay();			
				
			}
		}		
	}
		
	//时延		
	void delay() {
		try {
			Thread.sleep(Q8.delay);
		}
		catch(InterruptedException e){
		}
	}
	
	
	
}

⌨️ 快捷键说明

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