📄 queen2.java
字号:
/*
* Queen2.java
*
* Created on 2008年1月7日, 下午7:57
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package queen2;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.NumberFormat;
import java.net.*;
import java.lang.*;
/**
*
* @author Ling
*/
public class Queen2 {
/** Creates a new instance of Queen2 */
static int QueenMax=100;//定义皇后的个数QueenMax的初始值,所以程序最大可计算皇后为100,
static int oktimes = 0;// 每个皇后问题解的个数oktiemes
static int chess[] = new int[QueenMax];//每一个Queen的放置位置
public static String []rows=new String[99999];//保存算法的字符串,也决定了算法可以算多少后问题。这里设置得比较大。为了能算多后问题。
public static void main(String args[])//程序主方法
{
TextDlg a=new TextDlg();//调出第一个对话框
}
/*
*功能:获得每一种方法的字符串,保存在rows数组中,用的是递归方法实现回溯,在程序中调用placequeen自身。
*/
public static void placequeen(int num)
{ //num 为现在要放置的行数
int i=0;
boolean qsave[] = new boolean[QueenMax];
for(;i< QueenMax;i++)
qsave[i]=true;
i=0;
//下面先把安全位数组完成
//i 是现在要检查的数组值
while (i< num)
{
qsave[chess[i]]=false;
int k=num-i;
//检查该皇后行列对角线是否有另外一个皇后
if ( (chess[i]+k >= 0) && (chess[i]+k < QueenMax) )
qsave[chess[i]+k]=false;
if ( (chess[i]-k >= 0) && (chess[i]-k < QueenMax) )
qsave[chess[i]-k]=false;
i++;
}
//下面历遍安全位
for(i=0;i< QueenMax;i++)
{
if (qsave[i]==false)
continue;
if (num< QueenMax-1)//没有放完全部皇后,继续调用自身放皇后。
{
chess[num]=i;
placequeen(num+1);
}
else//皇后已经全部放好,记录一种解。解的种数++
{
chess[num]=i;
oktimes++;
String row="";//用一个String来记录皇后的位置。“+”为皇后的位置。
for (i=0;i< QueenMax;i++)
{
if (chess[i]==0);
else
for(int j=0;j< chess[i];j++) row+="-";
row+="+";
int j = chess[i];
while(j< QueenMax-1)
{
row+="-";
j++;
}
rows[oktimes-1]=row;
}
}
}
//历遍完成就停止
}
}
//用swing组件图形化显示
class TextDlg extends JFrame //输入皇后个数的对话框。
{
JPanel pan1;
JPanel pan2;
JPanel pan3;
JButton but1;
JButton but2;
JLabel L1;
JLabel Icon;
JTextField Input;
URL url1=TextDlg.class.getResource("imag1.jpg");
Icon icon1=new ImageIcon(url1);
static int n;
TextDlg()
{
super("N皇后问题");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
initialize();
}
public void initialize()//对话框的初始化
{
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel();
L1=new JLabel();
Icon=new JLabel(icon1);
but2=new JButton("使用说明");
but1=new JButton("确定");
Input=new JTextField(4);
pan1.setSize(50,50);
pan2.setSize(50,50);
pan3.setSize(50,50);
pan1.add(Input);
pan1.add(but1);
pan1.add(but2);
pan2.add(L1);
pan3.add(Icon);
setLocation(300,0);
pan1.setBackground(Color.BLACK);
pan2.setBackground(Color.BLACK);
pan3.setBackground(Color.BLACK);
L1.setText("请输入皇后个数:");
L1.setForeground(Color.WHITE);
setBackground(Color.BLACK);
setVisible(true);
getContentPane().add(pan3,BorderLayout.WEST);
getContentPane().add(pan1,BorderLayout.EAST);
getContentPane().add(pan2,BorderLayout.CENTER);
setSize(43*9,10*8);
getbut1();
getbut2();
}
private JButton getbut1() //两个按钮按下时分别执行的程序。
{
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
n=Integer.parseInt(Input.getText());
if(n>=4)
{
Queen2.QueenMax=n;
for (int i=0;i< Queen2.QueenMax;i++)
Queen2.chess[i]=-1;
Queen2.placequeen(0);
QueenTu application = new QueenTu ();
}
else
{
error err = new error();
}
}
});
return but1;
}
private JButton getbut2()
{
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
UserGuide ug = new UserGuide ();
}
});
return but2;
}
}
class QueenTu extends JFrame //皇后摆放显示对话框。
{
JLabel lab1=new JLabel("这是第1种解");
JLabel lab2=new JLabel(Queen2.QueenMax+"皇后问题共有"+Queen2.oktimes+"种解.");
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
JPanel pan3=new JPanel();
JButton but1=new JButton("下一解");
JButton but2=new JButton("清除");
JButton but3=new JButton("About..");
URL url1=QueenTu.class.getResource("imag1.jpg");
URL url2=QueenTu.class.getResource("imag2.jpg");
Icon icon1=new ImageIcon(url1);
Icon icon2=new ImageIcon(url2);
int size=Queen2.QueenMax*Queen2.QueenMax;
public JLabel[] lab=new JLabel[size];
int i=0;
QueenTu()
{
super("N皇后问题");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
initialize();
}
public void initialize()//初始华
{
pan1.setLayout(new GridLayout(Queen2.QueenMax,Queen2.QueenMax,2,2));
pan1.setSize(27*Queen2.QueenMax,38*Queen2.QueenMax);
pan2.setSize(50,50);
lab1.setForeground(Color.pink);
lab2.setForeground(Color.ORANGE);
for(int j=0;j< size;j++)
{
String str;
str=(Queen2.rows[0].substring(j,j+1)).trim();
if(str.equals("+"))
lab[j]=new JLabel(icon1);
else
lab[j]=new JLabel(icon2);
}
i++;
for(int i=0;i< size;i++)
pan1.add(lab[i]);
pan2.add(but1);
pan2.add(but2);
pan2.add(but3);
pan3.add(lab2);
pan3.add(lab1);
getContentPane().add(pan3,BorderLayout.NORTH);
getContentPane().add(pan1,BorderLayout.CENTER);
getContentPane().add(pan2,BorderLayout.SOUTH);
setLocation(300,0);
if(Queen2.QueenMax>=9){setSize(27*Queen2.QueenMax+50,38*Queen2.QueenMax+50);}
else{setSize(27*9+50,38*9+50);}
pan1.setBackground(Color.BLACK);
pan2.setBackground(Color.DARK_GRAY);
pan3.setBackground(Color.DARK_GRAY);
setBackground(Color.BLACK);
setVisible(true);
getbut1();
getbut2();
getbut3();
}
private JButton getbut1() //按钮事件
{
but1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(i< Queen2.oktimes)
{
for(int j=0;j< size;j++)
{
String str;
str=(Queen2.rows[i].substring(j,j+1)).trim();
if(str.equals("+"))
lab[j].setIcon(icon1);
else
lab[j].setIcon(icon2);
lab1.setText("这是第"+(i+1)+"种解法");
}
i++;
}
else
{
lab1.setText("这已经是最后一个解了~");
}
}
});
return but1;
}
private JButton getbut2()
{
but2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int j=0;j< size;j++)
{
lab[j].setIcon(icon2);
}
i=0;
lab1.setText("清除");}
});
return but2;
}
private JButton getbut3()
{
but3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
About about = new About();
}
});
return but3;
}
}
class About extends JFrame //关于作者的对话框
{
JPanel pan1;
JPanel pan2;
JPanel pan3;
JButton but1;
JLabel L1;
JLabel Icon;
JTextField Input;
URL url1=TextDlg.class.getResource("imag1.jpg");
Icon icon1=new ImageIcon(url1);
About()
{
super("About [N后问题]");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
initialize();
}
public void initialize()
{
pan1=new JPanel();
L1=new JLabel();
Icon=new JLabel(icon1);
pan1.setSize(50,50);
pan1.add(L1);
pan1.add(Icon);
setLocation(300,100);
pan1.setBackground(Color.BLACK);
L1.setText("@author 林凌 2005131346 计机1班");
L1.setForeground(Color.WHITE);
setBackground(Color.BLACK);
setVisible(true);
getContentPane().add(pan1,BorderLayout.CENTER);
setSize(35*9,10*8);
}
}
class UserGuide extends JFrame //使用说明的对话框。
{
JPanel pan1;
JTextArea L1;
JLabel Icon;
URL url1=TextDlg.class.getResource("imag1.jpg");
Icon icon1=new ImageIcon(url1);
UserGuide()
{
super("N皇后问题使用说明");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
initialize();
}
public void initialize()
{
pan1=new JPanel();
L1=new JTextArea();
Icon=new JLabel(icon1);
pan1.setSize(50,50);
pan1.add(L1);
pan1.add(Icon);
setLocation(300,100);
pan1.setBackground(Color.BLACK);
L1.setSize(50,100);
L1.setBackground(Color.BLACK);
L1.setText("请在空格中填入皇后的个数\n" +
"皇后的个数需为大于4的数,否则会出现错误提示.\n" +
"然后单击确定,即会弹出皇后摆放对话框\n" +
"每按一次'下一解'就会出现下一组解,\n" +
"按'清除'解的出现从头开始。\n" +
"按'About'出现作者信息。\n" +
"注:关闭任一弹出窗口,即关闭整个程序。\n" +
"\n最后,祝您使用愉快!\n版权所有:Ling\n深圳大学05级计算机1班\n");
L1.setEditable(false);
L1.setForeground(Color.WHITE);
setBackground(Color.BLACK);
setVisible(true);
getContentPane().add(pan1,BorderLayout.CENTER);
setSize(35*9,30*8);
}
}
class error extends JFrame //使用说明的对话框。
{
JPanel pan1;
JTextArea L1;
JLabel Icon;
URL url1=TextDlg.class.getResource("imag1.jpg");
Icon icon1=new ImageIcon(url1);
error()
{
super("出错了!");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
initialize();
}
public void initialize()
{
pan1=new JPanel();
L1=new JTextArea();
Icon=new JLabel(icon1);
pan1.setSize(50,50);
pan1.add(L1);
pan1.add(Icon);
setLocation(300,100);
pan1.setBackground(Color.BLACK);
L1.setSize(50,100);
L1.setBackground(Color.BLACK);
L1.setText("空格中填入的皇后个数\n" +
"必须为大于4的整数~\n" +
"请重新输入.\n" +
"\n祝您使用愉快!\n版权所有:Ling\n深圳大学05级计算机1班\n");
L1.setEditable(false);
L1.setForeground(Color.WHITE);
setBackground(Color.BLACK);
setVisible(true);
getContentPane().add(pan1,BorderLayout.CENTER);
setSize(35*9,22*8);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -