How to Use "L" dominos to conver 2^K * 2^K boxes

The code:

  1 package algorithm.study.demo1;
  2 
  3 import java.util.Arrays;
  4 
  5 import org.junit.Test;
  6 
  7 /**
  8  * This class is to implement how use "L" dominos to bespread the 2^K * 2^K boxes. But one of the
  9  * boxes has bean put a dominos
 10  * 
 11  * for example: there is a boxes and "0" represent the boxes need filled by "L" dominos
 12  * the "1" represent the box has bean filled by dominos.
 13  * [0, 1, 0, 0]
 14    [0, 0, 0, 0]
 15    [0, 0, 0, 0]
 16    [0, 0, 0, 0]
 17    
 18    There are four types "L" dominos to used
 19    
 20    a):          b):             c):          d):
 21        1 1          1 1              1          1
 22        1              1              1 1      1 1
 23      you will use the four types of domines to fill of empty boxes
 24    
 25  * 
 26  * @author ygh 2017年2月23日
 27  */
 28 public class LDomino {
 29     
 30     //The domino flag,same domina will use same number
 31     private int LDominos = 10;
 32     
 33     
 34     /**
 35      * Arithmetic thoughts:Using recursion thoughts
 36      * a):We first separate the all dominos boxes to four smaller and equal
 37      *  dominos,one of the four domines must has a box has bean fill;
 38      *  
 39      *  [0, 1,          0, 0]
 40         [0, 0,          0, 0]
 41         
 42         [0, 0,          0, 0]
 43         [0, 0,          0, 0]
 44      *  
 45      *  b):then we fill the center of four dominos with "L",but if one has
 46      *  the filled box,we will not to fill it.
 47      *  
 48      *  [0, 1,          0, 0]
 49         [0, 0,          1, 0]
 50         
 51         [0, 1,          1, 0]
 52         [0, 0,          0, 0]
 53         
 54       *
 55       *c):we use same method to separate the four dominos to smaller dominos,then fill the center boxes
 56       * except for one has a box been filled with
 57       * 
 58       * 
 59       * [3,     1,          2,          2]
 60       * 
 61         [3,     3,          1,          2]
 62         
 63         [3,     1,          1,          2]
 64         
 65         [3,     3,          2,          2]
 66         
 67         you see it success,so will use program to implement it following
 68       *
 69         
 70      * @param or The boxes primary row  行
 71      * @param oc The boxes primary line 列
 72      * @param dr The boxes has been fill with domino row 行
 73      * @param dc The boxes has been fill with domino line 列
 74      * @param size The size of the box,we will use 2^size as the box's row and line
 75      */
 76     public void fillBoxes(int[][] arr,int or,int oc,int dr,int dc,int size){
 77         //get boxes and initialize a box filled with domino
 78         
 79         if(size==1){
 80             return;
 81         }
 82         
 83         //the temporary number of dominos
 84         int filled = ++LDominos;
 85         //the size of smaller boxes
 86         int s = size / 2;
 87         
 88         //If the box filled in the top left
 89         if(dr<or+s && dc<oc+s){
 90             fillBoxes(arr,or,oc,dr,dc,s);
 91         }else{//If the box filled not in the top left
 92             arr[or+s-1][oc+s-1]=filled;
 93             fillBoxes(arr,or, oc, or+s-1, oc+s-1, s);
 94         }
 95         
 96         //If the box filled in the top right
 97         if(dr>=or+s && dc < oc+s){
 98             fillBoxes(arr,or+s, oc, dr, dc, s);
 99         }else{ //If the box filled not in the top right
100             arr[or+s][oc+s-1]=filled;
101             fillBoxes(arr,or+s, oc, or+s, oc+s-1, s);
102         }
103         
104         //If the box filled in the bottom left
105         if(dr<or+s && dc>=oc+s){
106             fillBoxes(arr,or, oc+s, dr, dc, s);
107         }else{//If the box filled not in the bottom left
108             arr[or+s-1][oc+s]=filled;
109             fillBoxes(arr,or, oc+s, or+s-1, oc+s, s);
110         }
111         
112         //If the box filled in the bottom right
113         if(dr>=or+s && dc>=oc+s){
114             fillBoxes(arr,or+s, oc+s, dr, dc, s);
115         }else{//If the box filled not in the bottom right
116             arr[or+s][oc+s]=filled;
117             fillBoxes(arr,or+s, oc+s, or+s, oc+s, s);
118         }
119         
120     }
121 
122     
123     /**
124      * return
125      * @param size
126      * @return
127      */
128     public int[][] getBoxes(int size) {
129         int arr [][] = new int[size][size];
130         for(int i=0;i<size;i++){
131             Arrays.fill(arr[i], 0); 
132         }
133         return arr;
134     }
135     
136     /**
137      * print the boxes
138      * @param arr The boxes needed to print
139      * @param size The size of boxes,the size must be 2^k
140      */
141     public void inputBoxes(int[][]arr,int size,String status){
142         System.out.println(status);
143         for(int i=0;i<size;i++){
144             System.out.println(Arrays.toString(arr[i]));
145         }
146     }
147     
148     
149     /**
150      * Get a random Integer,but we give it max value.
151      * 
152      * @param maxValue The max value of the random integer.
153      * @return A random integer that don't than max value.
154      */
155     public int getRandow(int maxValue) {
156         return (int) (Math.random() * maxValue);
157     }
158     
159     
160     /**
161      * test preparation
162      * @param size
163      */
164     public void prepareTest(int size){
165         int[][] arr = getBoxes(size);
166         int dr = getRandow(arr.length);
167         int dc = getRandow(arr.length);
168         //set one of the box filled as "10"
169         arr[dr][dc]=10;
170         inputBoxes(arr, size,"未处理状态:");
171         fillBoxes(arr, 0, 0, dr, dc, size);
172         inputBoxes(arr, size,"已处理状态:");
173         System.out.println("===================================");
174     }
175     
176     @Test
177     public void fun2(){
178         for(int i=0;i<=4;i++){
179             int size = (int) Math.pow(2, i);
180             prepareTest(size);
181         }
182     }
183     
184     /*
185      * The result of test
186      * 未处理状态:
187 [10]
188 已处理状态:
189 [10]
190 ===================================
191 未处理状态:
192 [0, 10]
193 [0, 0]
194 已处理状态:
195 [11, 10]
196 [11, 11]
197 ===================================
198 未处理状态:
199 [0, 0, 0, 0]
200 [0, 0, 10, 0]
201 [0, 0, 0, 0]
202 [0, 0, 0, 0]
203 已处理状态:
204 [13, 13, 15, 15]
205 [13, 12, 10, 15]
206 [14, 12, 12, 16]
207 [14, 14, 16, 16]
208 ===================================
209 未处理状态:
210 [0, 0, 0, 0, 0, 0, 0, 0]
211 [0, 0, 0, 0, 0, 0, 0, 0]
212 [0, 0, 0, 0, 0, 0, 0, 0]
213 [0, 0, 0, 0, 0, 0, 0, 0]
214 [0, 0, 0, 0, 0, 0, 0, 0]
215 [0, 0, 0, 0, 0, 0, 0, 0]
216 [0, 10, 0, 0, 0, 0, 0, 0]
217 [0, 0, 0, 0, 0, 0, 0, 0]
218 已处理状态:
219 [19, 19, 21, 21, 29, 29, 31, 31]
220 [19, 18, 18, 21, 29, 28, 28, 31]
221 [20, 18, 22, 22, 30, 30, 28, 32]
222 [20, 20, 22, 17, 17, 30, 32, 32]
223 [24, 24, 26, 26, 17, 34, 36, 36]
224 [24, 23, 23, 26, 34, 34, 33, 36]
225 [25, 10, 23, 27, 35, 33, 33, 37]
226 [25, 25, 27, 27, 35, 35, 37, 37]
227 ===================================
228 未处理状态:
229 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
230 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
231 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
232 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
233 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
234 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
235 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
236 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
237 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
238 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
239 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0]
240 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
241 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
242 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
243 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
244 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
245 已处理状态:
246 [41, 41, 43, 43, 51, 51, 53, 53, 83, 83, 85, 85, 93, 93, 95, 95]
247 [41, 40, 40, 43, 51, 50, 50, 53, 83, 82, 82, 85, 93, 92, 92, 95]
248 [42, 40, 44, 44, 52, 52, 50, 54, 84, 82, 86, 86, 94, 94, 92, 96]
249 [42, 42, 44, 39, 39, 52, 54, 54, 84, 84, 86, 81, 81, 94, 96, 96]
250 [46, 46, 48, 39, 56, 56, 58, 58, 88, 88, 90, 90, 81, 98, 100, 100]
251 [46, 45, 48, 48, 56, 55, 55, 58, 88, 87, 87, 90, 98, 98, 97, 100]
252 [47, 45, 45, 49, 57, 55, 59, 59, 89, 89, 87, 91, 99, 97, 97, 101]
253 [47, 47, 49, 49, 57, 57, 59, 38, 38, 89, 91, 91, 99, 99, 101, 101]
254 [62, 62, 64, 64, 72, 72, 74, 38, 104, 104, 106, 106, 114, 114, 116, 116]
255 [62, 61, 61, 64, 72, 71, 74, 74, 104, 103, 103, 106, 114, 113, 113, 116]
256 [63, 61, 65, 65, 73, 71, 71, 75, 105, 103, 107, 107, 115, 10, 113, 117]
257 [63, 63, 65, 60, 73, 73, 75, 75, 105, 105, 107, 102, 115, 115, 117, 117]
258 [67, 67, 69, 60, 60, 77, 79, 79, 109, 109, 111, 102, 102, 119, 121, 121]
259 [67, 66, 69, 69, 77, 77, 76, 79, 109, 108, 111, 111, 119, 119, 118, 121]
260 [68, 66, 66, 70, 78, 76, 76, 80, 110, 108, 108, 112, 120, 118, 118, 122]
261 [68, 68, 70, 70, 78, 78, 80, 80, 110, 110, 112, 112, 120, 120, 122, 122]
262 ===================================
263 
264      */
265     
266     
267 }

 

posted @ 2017-02-23 11:26  浪漫逆风  阅读(227)  评论(0编辑  收藏  举报