欢迎来到天天文库
浏览记录
ID:9362004
大小:133.00 KB
页数:12页
时间:2018-04-29
《java求解n皇后问题代码及答案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验题目:回溯法—n皇后问题 班级:信息112姓名:孙波学号:110111213日期:2014-5-13一、实验目的会用回溯法求解n皇后问题,通过实验掌握如何使用回溯法求解问题。二、实验要求用自己熟悉的高级程序设计语言编写程序实现求解n皇后问题的回溯算法。使用剪枝函数来避免不必要的搜索。三、源程序(带注释)importjava.applet.Applet;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.Graphics;importjava.aw
2、t.Graphics2D;importjava.awt.GridLayout;importjava.awt.TextArea;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.ItemEvent;importjava.awt.event.ItemListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;imp
3、ortjava.util.Hashtable;importjavax.swing.BorderFactory;importjavax.swing.ButtonGroup;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JRadioButton;importjavax.swing.JScrollPane;importjavax.swing.JSlider;importjavax.swing.
4、JTextField;importjavax.swing.event.ChangeEvent;importjavax.swing.event.ChangeListener;publicclassNQueenimplementsItemListener,ActionListener,ChangeListener{JFramef=newJFrame("NQUEENPROBLEMDEMO");Containercp=f.getContentPane();int[]x=newint[10];int[][]X=newint[10000][10];String[]str=newStrin
5、g[10000];intm=4,a=1,b=1,counter=0,j=1;Panel4panel4=newPanel4();TextAreat1=newTextArea(30,40);JTextFieldt2=newJTextField(10);JRadioButtonr1=newJRadioButton("4个");JRadioButtonr2=newJRadioButton("5个");JRadioButtonr3=newJRadioButton("6个");JRadioButtonr4=newJRadioButton("7个");JRadioButtonr5=newJ
6、RadioButton("8个");JButtonbutton1=newJButton("开始演示");JButtonbutton2=newJButton("暂停");JButtonbutton3=newJButton("清空");JSliderslider1=newJSlider(20,100);javax.swing.Timertimer1;/////////////////////////////////////////////窗口的布局//////////////////////////////////////publicNQueen(){cp.setLayout(n
7、ewBorderLayout(20,20));t2.setBorder(BorderFactory.createTitledBorder("解的个数:"));JScrollPanescrollPane1=newJScrollPane(t1);scrollPane1.setBorder(BorderFactory.createTitledBorder("展示所有解:"));JPanelpanel1=newJPanel();panel1.setLayout(newGridLayout(2,1,10,20))
此文档下载收益归作者所有