資源簡介
算法系列(一):棋盤覆蓋
博客地址:http://blog.csdn.net/qq_22145801/article/

代碼片段和文件信息
package?com.ncepu.sherly.chessboardcover;
import?java.awt.Color;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.border.LineBorder;
/**
?*?@author?Sherly-Liu?棋盤類
?*?@param?tr表示棋盤左上角行坐標
?*?@param?tc表示棋盤左上角列坐標
?*?@param?dr表示特殊方格的行坐標
?*?@param?dc表示特殊方格的列坐標
?*?@param?SIZE
?*????????????=2^k。棋盤的規格為2^k*2^k
?*?
?*/
public?class?Chessboard?extends?JPanel?{
int?k;?//?棋盤的規格為2^k*2^k
int?SIZE;?//?SIZE=2^k
int[][]?b;//?棋格棋盤規格為2^k*2^k個棋格
int?tile?=?1;//?骨牌編號
L_Chess[]?chess;//?L型骨牌個數為?(4^k-1)/3
int?dr;//?表示特殊方格的行坐標
int?dc;//?表示特殊方格的列坐標
//?構造方法
public?Chessboard(int?k?int?dr?int?dc)?{
this.k?=?k;
this.dr?=?dr;
this.dc?=?dc;
SIZE?=?(int)?Math.pow(2?k);
b?=?new?int[SIZE][SIZE];//?申請n*n數組空間
int?temp?=?(((int)?Math.pow(4?k))?-?1)?/?3;//?L型骨牌個數為?(4^k-1)/3
chess?=?new?L_Chess[temp];
Fill(0?0?SIZE?dr?dc);
InitUI();
}
/**
?*?Fill方法:每一個L型骨牌用相等的數字填充。從1,2,3,......,(0號為特殊方塊所在位置),按左上、右上、右下、左下順序依次填充
?*/
public?void?Fill(int?tr?int?tc?int?size?int?dr?int?dc)?{
if?(size?==?1)
return;
else?{
int?t?=?tile++;
int?s?=?size?/?2;//?將棋盤四等分,即將問題分解為四個SIZE=2^(k-1)的子問題
if?((dr? //?b[tr?+?s?-?1][tc?+?s]?=?b[tr?+?s][tc?+?s]?=?b[tr?+?s][tc?+?s
//?-?1]?=?t;//?覆蓋第一個L型骨牌:在右上、右下、左下交界處
chess[t?-?1]?=?new?L_Chess(tr?+?s?-?1?tc?+?s?tr?+?s?tc?+?s
tr?+?s?tc?+?s?-?1?t);//?覆蓋第一個L型骨牌:在右上、右下、左下交界處
//?采用遞歸分別求解每個子問題
Fill(tr?tc?s?dr?dc);
Fill(tr?tc?+?s?s?tr?+?s?-?1?tc?+?s);
Fill(tr?+?s?tc?+?s?s?tr?+?s?tc?+?s);
Fill(tr?+?s?tc?s?tr?+?s?tc?+?s?-?1);
}?else?if?((dr?=?tc?+?s))?{//?特殊方格在右上子棋盤
//?b[tr?+?s?-?1][tc?+?s?-?1]?=?b[tr?+?s][tc?+?s?-?1]?=?b[tr?+
//?s][tc
//?+?s]?=?t;//?覆蓋第一個L型骨牌:在左上、左下、右下交界處
chess[t?-?1]?=?new?L_Chess(tr?+?s?-?1?tc?+?s?-?1?tr?+?s?tc
+?s?-?1?tr?+?s?tc?+?s?t);//?覆蓋第一個L型骨牌:在左上、左下、右下交界處
//?采用遞歸分別求解每個子問題
Fill(tr?tc?s?tr?+?s?-?1?tc?+?s?-?1);
Fill(tr?tc?+?s?s?dr?dc);
Fill(tr?+?s?tc?+?s?s?tr?+?s?tc?+?s);
Fill(tr?+?s?tc?s?tr?+?s?tc?+?s?-?1);
}?else?if?((dr?>=?tr?+?s)?&&?(dc?>=?tc?+?s))?{//?特殊方格在右下子棋盤
//?b[tr?+?s?-?1][tc?+?s?-?1]?=?b[tr?+?s?-?1][tc?+?s]?=?b[tr?+
//?s][tc
//?+?s?-?1]?=?t;//?覆蓋第一個L型骨牌:在右上、左上、左下交界處
chess[t?-?1]?=?new?L_Chess(tr?+?s?-?1?tc?+?s?-?1?tr?+?s?-?1
tc?+?s?tr?+?s?tc?+?s?-?1?t);//?覆蓋第一個L型骨牌:在右上、左上、左下交界處
//?采用遞歸分別求解每個子問題
Fill(tr?tc?s?tr?+?s?-?1?tc?+?s?-?1);
Fill(tr?tc?+?s?s?tr?+?s?-?1?tc?+?s);
Fill(tr?+?s?tc?+?s?s?dr?dc);
Fill(tr?+?s?tc?s?tr?+?s?tc?+?s?-?1);
}?else?if?((dr?>=?tr?+?s)?&&?(dc? //?b[tr?+?s?-?1][tc?+?s?-?1]?=?b[tr?+?s?-?1][tc?+?s]?=?b[tr?+
//?s][tc
//?+?s]?=?t;//?覆蓋第一個L型骨牌:在左上、右上、右下交界處
chess[t?-?1]?=?new?L_Chess(tr?+?s?-?1?tc?+?s?-?1?tr?+?s?-?1
tc?+?s?tr?+?s?tc?+?s?t);//?覆蓋第一個L型骨牌:
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-11-18?23:01??SuanFa\
?????文件?????????232??2016-06-16?11:16??SuanFa\.classpath
?????文件?????????382??2015-12-22?20:47??SuanFa\.project
?????目錄???????????0??2016-11-18?23:01??SuanFa\.settings\
?????文件??????????85??2016-09-23?10:15??SuanFa\.settings\org.eclipse.core.resources.prefs
?????文件?????????670??2016-09-17?10:25??SuanFa\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2016-11-18?23:03??SuanFa\bin\
?????目錄???????????0??2016-11-18?23:01??SuanFa\drawable\
?????文件????????5482??2013-10-20?12:30??SuanFa\drawable\1.gif
?????文件????????3801??2013-10-20?12:34??SuanFa\drawable\2.gif
?????文件????????2381??2013-10-20?12:35??SuanFa\drawable\3.gif
?????文件??????681215??2016-11-10?13:25??SuanFa\drawable\background.jpg
?????文件??????911425??2015-12-28?17:28??SuanFa\drawable\background2.jpg
?????目錄???????????0??2016-11-18?23:01??SuanFa\src\
?????目錄???????????0??2016-11-18?23:01??SuanFa\src\com\
?????目錄???????????0??2016-11-18?23:02??SuanFa\src\com\ncepu\
?????目錄???????????0??2016-11-18?23:01??SuanFa\src\com\ncepu\sherly\
?????目錄???????????0??2016-11-18?23:01??SuanFa\src\com\ncepu\sherly\chessboardcover\
?????文件????????5532??2016-11-18?22:44??SuanFa\src\com\ncepu\sherly\chessboardcover\Chessboard.java
?????文件???????????0??2016-11-18?22:52??SuanFa\src\com\ncepu\sherly\chessboardcover\ClassDiagram.mgc
?????文件????????2214??2016-11-18?22:44??SuanFa\src\com\ncepu\sherly\chessboardcover\L_Chess.java
?????文件????????7759??2016-11-18?22:06??SuanFa\src\com\ncepu\sherly\chessboardcover\Main.java
?????文件?????????671??2016-11-18?22:44??SuanFa\src\com\ncepu\sherly\chessboardcover\MyLabel.java
- 上一篇:Spring AOP的AspectJ支持jar包
- 下一篇:酒店管理系統源碼課程設計
評論
共有 條評論