1、问题描述:
在一个2k×2k个方格组成的棋盘中,若有一个方格与其他方格不同,则称该方格为一特殊方格,且称该棋盘为一个特殊棋盘.显然特殊方格在棋盘上出现的位置有种情形.因而对任何
k≥0,有4k种不同的特殊棋盘.图2-4中的特殊棋盘是当k=2时16个特殊棋盘中的一个.
2、编程任务:
在棋盘覆盖问题中,要用图2-5所示的4种不同形态的L型骨牌覆盖一个给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖.易知,在任何一个2k×2k的棋盘覆盖中,用到的L型骨牌个数恰为(4k-1)/3.
//旧巷里的旧少年****************
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
public class demo {
//变量定义部分
int title;
int x;int y;//特殊方格行列号
int Size;//方格宽度
JLabel [][] cube;//小方格数组
JPanel LabelPanel;
JTextField size;
JTextField xCount;
JTextField yCount;
JButton sumbit;
JFrame frame;
JLabel label1;
Color colors[]={Color.green,Color.BLACK,Color.BLUE,Color.cyan,Color.ORANGE,Color.PINK,Color.yellow};
int cColor=0;//用于colors数组计数
demo(){
title=0;
frame=new JFrame();
Container contentPane=frame.getContentPane();
contentPane.setLayout(new GridLayout(3,1));
JPanel buttonPanel=new JPanel();
buttonPanel.setLayout(new GridLayout(3,2));
label1=new JLabel("棋盘方格宽度:",JLabel.CENTER);
label1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
size=new JTextField("4");
JLabel label2=new JLabel("请输入特殊方格横坐标:",JLabel.CENTER);
label2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
xCount=new JTextField("0");
JLabel label3=new JLabel("请输入棋盘方格纵坐标:",JLabel.CENTER);
label3.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
yCount=new JTextField("0");
buttonPanel.add(label1);
buttonPanel.add(size);
buttonPanel.add(label2);
buttonPanel.add(xCount);
buttonPanel.add(label3);
buttonPanel.add(yCount);
buttonPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
//旧巷里的旧少年****************
cube=new JLabel[20][20];
LabelPanel=new JPanel();
LabelPanel.setLayout(new GridLayout(4,4));
LabelPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2),
"棋盘",TitledBorder.CENTER,TitledBorder.TOP));
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
cube[i][j]=makelabel();
LabelPanel.add(cube[i][j]);
}
sumbit=new JButton("点击刷新确认棋盘");
JPanel sumbitPanel=new JPanel();
sumbitPanel.add(sumbit);
sumbitPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.add(LabelPanel);
contentPane.add(buttonPanel);
contentPane.add(sumbitPanel);
sumbit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("点击刷新确认棋盘"))
{
String str=new String();
str = xCount.getText();
x=Integer.parseInt(str);
str = yCount.getText();
y=Integer.parseInt(str);
str=size.getText();
Size=Integer.parseInt(str);
//旧巷里的旧少年****************
LabelPanel.removeAll();
// String str=new String();
str = size.getText();
Size=Integer.parseInt(str);
LabelPanel.setLayout(new GridLayout(Size,Size));
for(int i=0;i<Size;i++)
for(int j=0;j<Size;j++)
{
if(i==x-1&&j==y-1)
{
cube[i][j]=makelabel();
cube[i][j].setText("特殊");
}
else
cube[i][j]=makelabel();
LabelPanel.add(cube[i][j]);
}
// cube[i-1][j-1]=makelabel();
// cube[i-1][j-1].setText("特殊");
// label1.setText(label1.getText()+"
"+"您输入的棋盘如上");
frame.pack();
frame.setBounds(new Rectangle(500, 200, 500, 400));
if(x>Size||y>Size||x<=0||y<=0){
int type=JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(frame, new String("您输入的特殊方格不合法,请重新输入!(提示特殊方格索引从1开始)"), new String("Error"), type);
sumbit.setText("点击刷新确认棋盘");
}
else
{
xCount.setEditable(false);
size.setEditable(false);
yCount.setEditable(false);
sumbit.setText("开始计算");
}
}
if(e.getActionCommand().equals("开始计算"))
{
String str=new String();
str = xCount.getText();
x=Integer.parseInt(str)-1;
str = yCount.getText();
y=Integer.parseInt(str)-1;
str = size.getText();
Size=Integer.parseInt(str);
title=0;
chessBoard(0,0,x,y,Size);
cube[x][y].setOpaque(true);
cube[x][y].setBackground(Color.WHITE);
cube[x][y].setText("特殊");
sumbit.setText("点击刷新确认棋盘");
xCount.setEditable(true);
size.setEditable(true);
yCount.setEditable(true);
}
}
});
//旧巷里的旧少年****************
frame.pack();
frame.setVisible(true);
// frame.setSize(300, 500);
frame.setBounds(new Rectangle(500, 200, 500, 400));
frame.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
void chessBoard(int tr,int tc, int dr,int dc,int size)
{
if(size==1)return;
int t=++title;
int s=size/2;
String str=Integer.toString(t);
int ci=((cColor++)%7);
if(dr<tr+s&&dc<tc+s)
{
chessBoard(tr,tc,dr,dc,s);
}
else
{
cube[tr+s-1][tc+s-1].setOpaque(true);
cube[tr+s-1][tc+s-1].setBackground(colors[ci]);
cube[tr+s-1][tc+s-1].setText(str);
chessBoard(tr,tc,tr+s-1,tc+s-1,s);
}
if(dr<tr+s&&dc>=tc+s)
{
chessBoard(tr,tc+s,dr,dc,s);
}
else
{
cube[tr+s-1][tc+s].setOpaque(true);
cube[tr+s-1][tc+s].setBackground(colors[ci]);
cube[tr+s-1][tc+s].setText(str);
chessBoard(tr,tc+s,tr+s-1,tc+s,s);
}
if(dr>=tr+s&&dc<tc+s)
{
chessBoard(tr+s,tc,dr,dc,s);
}
else
{
cube[tr+s][tc+s-1].setOpaque(true);
cube[tr+s][tc+s-1].setBackground(colors[ci]);
cube[tr+s][tc+s-1].setText(str);
chessBoard(tr+s,tc,tr+s,tc+s-1,s);
}
if(dr>=tr+s&&dc>=tc+s)
{
chessBoard(tr+s,tc+s,dr,dc,s);
}
else
{
cube[tr+s][tc+s].setOpaque(true);
cube[tr+s][tc+s].setBackground(colors[ci]);
cube[tr+s][tc+s].setText(str);
chessBoard(tr+s,tc+s,tr+s,tc+s,s);
}
}
public JLabel makelabel()
{
JLabel l=new JLabel("0",JLabel.CENTER);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
l.setPreferredSize(new Dimension(5,5));
return l;
}
public static void main(String []argv)
{
new demo();
}
}