The Java implementation of the thought of division and treatment in Trimino jigsaw puzzle

Source: Internet
Author: User

The recent study of the basic algorithm design and analysis of the second edition, Learning to divide and conquer the law, after the class exercise attracted, that is, trimino puzzles. Thought for a long time, did not think how to divide and conquer. Then it is Google to the relevant PPT. It's clear at first sight. I use the code to achieve the next. After understanding the idea, the code implementation is very easy.

This puzzle can actually be made into a small puzzle game.

The key to divide and conquer thought is to construct the same sub-problem. This is the core of the idea that seems simple, but actually requires insight into the problem. See the PPT core diagram as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/49/D7/wKiom1Qb56bBi3IIAAD685oXOWY186.jpg "style=" float: none; "title=" Trimino_1.png "alt=" Wkiom1qb56bbi3iiaad685oxowy186.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/49/D7/wKiom1Qb56bxqvNVAAEPlWMk2wA434.jpg "style=" float: none; "title=" Trimino_2.png "alt=" Wkiom1qb56bxqvnvaaeplwmk2wa434.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/49/D8/wKioL1Qb58Lx3jQHAAE7MfILbzQ828.jpg "style=" float: none; "title=" Trimino_3.png "alt=" Wkiol1qb58lx3jqhaae7mfilbzq828.jpg "/>

The source code for the algorithm is as follows:

/** *  @author  shuaiguangying * To solve the problem of board coverage by division and treatment  *   there is a bad point in the 2^n*2^n board  *    bad points can be anywhere  *   cover the board with the L-plate  *   *   The key to solving this problem is: Divide and conquer  * The key to    division is to construct a checkerboard with a bad point  *   */public class tromino {static int  numOfL = 2;/** *  @param  a  Board  *  @param  x  starting position of the sub-chessboard   *  @param  y  Sub-board start  *  @param  size  size of sub-board  *  @param  bx  Location of the bad points in the checkerboard  *  @param  by  location of the bad points in the  */public static void trimino (integer[] [] a,int x,int y,int size,int bx,int by] {//to divide a large image into 4 small figures INT&NBSP;NX&NBSP;=X+SIZE/2 , ny =y+size/2;//Cross Center int[] bx4 = {nx  ,nx-1,nx-1,nx};int[] by4 = { Ny-1,ny-1,ny  ,ny};if (BY&LT;NY&AMP;&AMP;BX&GT;=NX) { //bad point in first quadrant Bx4[0]=bx;by4[0]=by;} Else if (BY&LT;NY&AMP;&AMP;BX&LT;NX) {//bad points in the second quadrant Bx4[1]=bx;by4[1]=by;} Else if (BY&GT;=NY&AMP;&AMP;BX&LT;NX) {//bad points in the third quadrant Bx4[2]=bx;by4[2]=by;} else{//bad point in fourth quadrant Bx4[3]=bx;by4[3]=by;} Put the L-shaped block diagram into the checkerboard for (int i=0;i<4;i++) {if (BY4[I]==BY&NBSP;&AMP;&AMP;&NBSP;BX4[I]==BX) continue;a[by4[i]][bx4[ I]]=NUMOFL;} numofl++;//Division   Recursive processing sub-problem if (size>2) {Trimino (a,nx,y,size/2,bx4[0],by4[0]); Trimino (a,x,y,size/2,bx4[1],by4[1 ]); Trimino (a,x,ny,size/2,bx4[2],by4[2]); Trimino (A,nx,ny,size/2,bx4[3],by4[3]);}} Public static void main (String[] args)  {int size = 8;Integer[][]  A=new integer[size][size];int bx=7,by=1;a[by][bx]=1;trimino (A,0,0,size,bx,by);d Raw (a,size);} Private static void draw (integer[][] a,int size) {jframe f = new  JFrame ("Trimino");integer[] head = new integer[size];for  (int i=0;i< head.length;i++)  head[i] = i+1;final color[] color =new color[22];for (int  i=0;i<color.length;i++) {int r =  (int)   ((Math.random () *10000)%255);  int b = (int)   ((Math.random () *1000)%255)  int k = (int)   ((Math.random () *10000)%255);  color[i]  = new color (r,b,k);} Jtable table = new jtable (A,head);for  (int i = 0; i <  table.getcolumncount ();  i++)  {tablecolumn column = table.getcolumn ( String.valueof (i+1)); Column.setcellrenderer (New defaulttablecellrenderer () {@Overridepublic  component  gettablecellrenderercomponent (jtable table,object value, boolean isselected,  Boolean hasfocus,int row, int column)  {int val =  (Integer) value; SetBackground (Color[val-1]); Return super.gettablecellrenderercomponent (table, value, isselected ,  hasfocus,row, column);});} Table.setautoresizemode (Jtable.auto_resize_off); Table.setrowheight (f);. Getcontentpane (). Add (Table.gettableheader (), Borderlayout.north), F.getcontentpane (). Add (table, Borderlayout.center); f.setvisible (true); F.setsize (500, 500);}}

To demonstrate the effectiveness of the algorithm, a graph was drawn:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/49/D8/wKioL1Qb5kLjZO3vAAFn3mmnpjc607.jpg "title=" Trimino.png "alt=" Wkiol1qb5kljzo3vaafn3mmnpjc607.jpg "/>


This article is from a "little progress every Day" blog, make sure to keep this source http://sbp810050504.blog.51cto.com/2799422/1555206

The Java implementation of the thought of division and treatment in Trimino jigsaw puzzle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.