TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
-
- 八皇后问题图形版:
- //8 Queen 递归算法
- //如果有一个Q 为 chess[i]=j;
- //则不安全的地方是 k行 j位置,j+k-i位置,j-k+i位置
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import java.net.*;
- public class Queen2{
- //定义皇后的个数QueenMax,算法的种类oktiemes
- static final int QueenMax = 8;
- static int oktimes = 0;
- static int chess[] = new int[QueenMax];//每一个Queen的放置位置
- public static String []rows=new String[92];//保存算法的字符串
- public static void main(String args[])
- {
- for (int i=0;i< QueenMax;i++)
- chess[i]=-1;
- placequeen(0);
- QueenTu queen=new QueenTu();
- System.out.println("
- 八皇后共有"+oktimes+"个解法. 作者:孟雪锋、张锐 版本:1.0");
- }
- /*
- *@功能就是获得每一种方法的字符串,保存在rows数组中,用的是递归方法
- */
- 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
- { //num is last one
- chess[num]=i;
- oktimes++;
- //System.out.println("这是第"+oktimes+"个解法 如下:");
- //System.out.println("第n行: 1 2 3 4 5 6 7 8");
- String row="";
- 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 QueenTu extends JFrame
- {
- JLabel lab1=new JLabel("第1种解法");
- JPanel pan1=new JPanel();
- JPanel pan2=new JPanel();
- JPanel pan3=new JPanel();
- JButton but1=new JButton("显示");
- JButton but2=new JButton("清除");
-
- 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("八皇后");
- 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,86*Queen2.QueenMax);
- pan2.setSize(50,50);
- lab1.setForeground(Color.red);
-
-
- 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(icon2);
- else
- lab[j]=new JLabel(icon1);
- }
- i++;
- but1.addActionListener(new Listener());
- but2.addActionListener(new Listener());
- for(int i=0;i< size;i++)
- pan1.add(lab[i]);
- pan2.add(but1);
- pan2.add(but2);
- pan3.add(lab1);
- getContentPane().add(pan3,BorderLayout.NORTH);
- getContentPane().add(pan1,BorderLayout.CENTER);
- getContentPane().add(pan2,BorderLayout.SOUTH);
- setLocation(300,0);
- setSize(27*Queen2.QueenMax+50,86*Queen2.QueenMax+50);
- pan1.setBackground(Color.ORANGE);
- pan2.setBackground(Color.YELLOW);
- pan3.setBackground(Color.green);
- setBackground(Color.MAGENTA);
- setVisible(true);
- }
-
- class Listener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
-
- if(e.getSource()==but1)
-
- 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(icon2);
- else
- lab[j].setIcon(icon1);
- lab1.setText("第"+(i+1)+"种解法");
- }
- i++;
- }
- else
- {
- i=0;
- for(int j=0;j< size;j++)
- {
- String str;
- str=(Queen2.rows[i].substring(j,j+1)).trim();
-
- if(str.equals("+"))
- lab[j].setIcon(icon2);
- else
- lab[j].setIcon(icon1);
- lab1.setText("第"+(i+1)+"种解法");
- }
- i++;
- }
- if(e.getSource()==but2)
- {
- for(int j=0;j< size;j++)
- {
- lab[j].setIcon(icon1);
- }
- i=0;
- lab1.setText("清除");
- }
-
- }
- }
- }
- 运行图:
- [img]http://img.javaxxz.com/2014/11/8/000504640.gif[/img]
-
复制代码
源码下载:http://file.javaxxz.com/2014/11/8/000504796.zip |
|