Triomino问题的动态演示程序。
源代码:
/**
* 此程序用于解决的问题是:用一个L形的瓦片(有三个小正方形组成)覆盖一个缺少了一个方块(可以是棋盘上的
* 任何位置)的2^n X 2^n棋盘
*/
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import java.awt.event.*;
public class Triomino extends JApplet implements ActionListener , Runnable
{
private JLabel lx;
private JLabel ly;
private JTextField tx;
private JTextField ty;
private JButton begin;
private JButton stop;
private Image image0;
private Image image1;
private Thread demo;
private int x,y;
int chessBoard[][] = new int [9][9];//定义一个8 x 8的棋盘
//private int x,y;
public void init()
{
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
chessBoard[i][j] = 0;
Container content = getContentPane();
content.setLayout(new BorderLayout());
JPanel cotrlPane = new JPanel();
cotrlPane.setLayout(new FlowLayout());
//setLayout(null);
lx = new JLabel("x:");
cotrlPane.add(lx);
tx = new JTextField(10);
cotrlPane.add(tx);
ly = new JLabel("y:");
cotrlPane.add(ly);
ty = new JTextField(10);
cotrlPane.add(ty);
begin = new JButton("开始");
begin.addActionListener(this);
cotrlPane.add(begin);
stop = new JButton("清除");
stop.addActionListener(this);
cotrlPane.add(stop);
content.add(cotrlPane, BorderLayout.SOUTH);
}
/**
* 0:表示用蓝色方块覆盖棋盘
* 1:表示用红色方块覆盖棋盘
* 2:表示用绿色方块覆盖棋盘
* 3:表示用黑色方块覆盖棋盘,棋盘上被标记为黄色的方块,表示棋盘被分为四部分以后,最中间的四个方块
* 中,将其中三个所在的部分不包含缺少方块的方块,标记为黑色
* 4:表示用黄色方块覆盖棋盘,棋盘上被标记为黄色的方块,表示缺少的那个方块
*/
public void paint(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
Rectangle2D.Float rectangle;
for(int i=1; i<=8; i++)
for(int j =1; j<=8; j++)
{
switch(chessBoard[i][j])
{
case 0:
{
g2D.setPaint(Color.blue);
break;
}
case 1:
{
g2D.setPaint(Color.red);
break;
}
case 2:
{
g2D.setPaint(Color.green);
break;
}
case 3:
{
g2D.setPaint(Color.black);
break;
}
case 4:
{
g2D.setPaint(Color.yellow);
break;
}
default:break;
}
//g2D.setPaint(Color.blue);
rectangle = new Rectangle2D.Float(10.0f+18*j, 40.0f+18*i, 15.0f, 15.0f);
//g2D.draw(rectangle);
//g2D.setPaint(Color.red);
g2D.fill(rectangle);
}
/*int x = 10+18*(int)(7*Math.random());
int y = 40+18*(int)(7*Math.random());
g2D.setPaint(Color.red);
g2D.fill(new Rectangle2D.Float(x,y, 15.0f, 15.0f));*/
lx.repaint();
ly.repaint();
tx.repaint();
ty.repaint();
begin.repaint();
stop.repaint();
}
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
if(action.equals("开始"))
{
x = Integer.parseInt(tx.getText());
y = Integer.parseInt(ty.getText());
chessBoard[x][y] = 4;
repaint();
fillRect(x, y, Color.yellow);
//trio(x, y, 1, 8, 1, 8);
if(demo == null || !demo.isAlive())
{
demo = new Thread(this);
demo.start();
}
}
if(action.equals("清除"))
{
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
chessBoard[i][j] = 0;
repaint();
tx.setText("");
ty.setText("");
if(demo != null || demo.isAlive())
demo = null;
//setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
/**
*用分治法解triomino问题
*/
public void trio(int x, int y, int cStart, int cEnd, int rStart, int rEnd)
{
if(cEnd - cStart > 1)
{
if(x>=cStart && x<=(cEnd+cStart)/2 && y>=rStart && y<=(rEnd+rStart)/2)
{
trio(x, y, cStart, (cEnd+cStart)/2, rStart, (rEnd+rStart)/2);
trio((cEnd+cStart)/2+1, (rEnd+rStart)/2, (cEnd+cStart)/2+1, cEnd,
rStart, (rEnd+rStart)/2);
trio((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, (cEnd+cStart)/2+1, cEnd,
(rEnd+rStart)/2+1, rEnd);
trio((cEnd+cStart)/2, (rEnd+rStart)/2+1, cStart, (cEnd+cStart)/2,
(rEnd+rStart)/2+1, rEnd);
/*fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2, Color.black);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, Color.black);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2+1, Color.black);*/
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2] = 3;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2+1] = 3;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2+1] = 3;
pause();
repaint();
}
if(x<=cEnd && x>(cEnd+cStart)/2 && y>=rStart && y<=(rEnd+rStart)/2)
{
trio(x, y, (cEnd+cStart)/2+1, cEnd, rStart, (rEnd+rStart)/2);
trio((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, (cEnd+cStart)/2+1, cEnd,
(rEnd+rStart)/2+1, rEnd);
trio((cEnd+cStart)/2, (rEnd+rStart)/2+1, cStart, (cEnd+cStart)/2,
(rEnd+rStart)/2+1, rEnd);
trio((cEnd+cStart)/2, (rEnd+rStart)/2, cStart, (cEnd+cStart)/2,
rStart, (rEnd+rStart)/2);
/*fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, Color.black);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2+1,Color.black);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2, Color.black);*/
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2+1] = 3;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2+1] = 3;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2] = 3;
pause();
repaint();
}
if(x<=cEnd && x>(cEnd+cStart)/2 && y<=rEnd && y>(rEnd+rStart)/2)
{
trio(x, y, (cEnd+cStart)/2+1, cEnd, (rEnd+rStart)/2+1, rEnd);
trio((cEnd+cStart)/2, (rEnd+rStart)/2+1, cStart, (cEnd+cStart)/2,
(rEnd+rStart)/2+1, rEnd);
trio((cEnd+cStart)/2, (rEnd+rStart)/2, cStart, (cEnd+cStart)/2,
rStart, (rEnd+rStart)/2);
trio((cEnd+cStart)/2+1, (rEnd+rStart)/2, (cEnd+cStart)/2+1, cEnd,
rStart, (rEnd+rStart)/2);
/*fillRect((cEnd+cStart)/2, (rEnd+rStart)/2+1, Color.black);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2, Color.black);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2, Color.black);*/
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2+1] = 3;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2] = 3;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2] = 3;
pause();
repaint();
}
if(x>=cStart && x<=(cEnd+cStart)/2 && y<=rEnd && y>(rEnd+rStart)/2)
{
trio(x, y, cStart, (cEnd+cStart)/2, (rEnd+rStart)/2+1, rEnd);
trio((cEnd+cStart)/2, (rEnd+rStart)/2, cStart, (cEnd+cStart)/2,
rStart, (rEnd+rStart)/2);
trio((cEnd+cStart)/2+1, (rEnd+rStart)/2, (cEnd+cStart)/2+1, cEnd,
rStart, (rEnd+rStart)/2);
trio((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, (cEnd+cStart)/2+1, cEnd,
(rEnd+rStart)/2+1, rEnd);
/*fillRect((cEnd+cStart)/2, (rEnd+rStart)/2, Color.black);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2, Color.black);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, Color.black);*/
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2] = 3;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2] = 3;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2+1] = 3;
pause();
repaint();
}
}
else
{
if(x>=cStart && x<=(cEnd+cStart)/2 && y>=rStart && y<=(rEnd+rStart)/2)
{
/*fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2, Color.red);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, Color.red);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2+1, Color.red);*/
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2] = 1;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2+1] = 1;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2+1] = 1;
pause();
repaint();
}
if(x<=cEnd && x>(cEnd+cStart)/2 && y>=rStart && y<=(rEnd+rStart)/2)
{
/*fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, Color.green);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2+1,Color.green);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2, Color.green);*/
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2+1] = 2;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2+1] = 2;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2] = 2;
pause();
repaint();
}
if(x<=cEnd && x>(cEnd+cStart)/2 && y<=rEnd && y>(rEnd+rStart)/2)
{
/*fillRect((cEnd+cStart)/2, (rEnd+rStart)/2+1, Color.red);
fillRect((cEnd+cStart)/2, (rEnd+rStart)/2, Color.red);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2, Color.red);*/
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2+1] = 1;
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2] = 1;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2] = 1;
pause();
repaint();
}
if(x>=cStart && x<=(cEnd+cStart)/2 && y<=rEnd && y>(rEnd+rStart)/2)
{
/*fillRect((cEnd+cStart)/2, (rEnd+rStart)/2, Color.green);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2, Color.green);
fillRect((cEnd+cStart)/2+1, (rEnd+rStart)/2+1, Color.green);*/
chessBoard[(cEnd+cStart)/2][(rEnd+rStart)/2] = 2;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2] = 2;
chessBoard[(cEnd+cStart)/2+1][(rEnd+rStart)/2+1] = 2;
pause();
repaint();
}
}
}
public void run()
{
trio(x, y, 1, 8, 1, 8);
}
public void pause()
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e){}
}
public void fillRect(int x, int y, Color color)
{
Graphics2D g2D = (Graphics2D)getGraphics();
g2D.setPaint(color);
g2D.fill(new Rectangle2D.Float(10.0f+18*x, 40.0f+18*y, 15.0f, 15.0f));
}
}