Topic:
There is a checkerboard with a width of W, a height of H, and a total small lattice of w*h. In the board, can be painted small lattice with "P" logo, can not paint the small lattice with "B" logo, you can paint or choose not to paint the small lattice with "?" Identity. The following w=5, H=4 's chessboard
Pppbb
Pp? Bb
Pp? Pp
BBP? P
There is a silly x, the hands have 1*1, 2*2, 3*3 and other square stamps. This silly x wants to use the largest stamp in the hand to cover all the squares of the board that can be painted with the P mark. where "?" The identified cells can be painted or not painted.
Above the w=5, H=4 's chessboard, can be painted area full of stamps for 3*3
Ask: Silly X with the largest stamp, you can put all the P-grid color, return to the edge of the stamp length
Idea 1: Hard traversal + reject invalid solution
First, we first find the horizontal continuous area, and take the smallest one.
Then take the minimum of the smallest contiguous region as the largest stamp
From the upper left corner of the seal: If you can cover, cover. No words to move a position in order to continue the cover
After the final seal is completed, check if there is no p lattice is not covered
Code:
Public Static BooleanCanbepaint (Char[] Canvas,intXintYintSize) { for(intI=x; I+size < canvas.length-1; i++){ for(intJ=y; J+size < canvas[i].length-1; J + +){ if(Canvas[i][j] = = ' B '){ return false; } } } return true; } Public Static Char[] Paint (Char[] Canvas,intXintYintSize) { for(intI=x; I+size < canvas.length-1; i++){ for(intJ=y; J+size < canvas[i].length-1; J + +) {Canvas[i][j]= ' # '; } } returnCanvas; } Public Static BooleanCheckallpaint (Char[] [] Canvas) { for(intx=0; x< canvas.length; X + +){ for(inty=0; Y < Canvas[x].length; y++){ if(Canvas[x][y] = = ' P '){ return false; } } } return true; } Public Static intGetmaxlength (Char[] [] Canvas) { intImaxtotal=0; intImaxw=canvas[0].length; intImaxh=canvas.length; intImaxline = 0; intICounter = 0; intPcounter = 0; for(inth=0; h< canvas.length; h++) {Imaxline= 0; ICounter= 0; Pcounter= 0; for(intw=0; w< canvas[h].length; w++){ if(Canvas[h][w] = = ' P ' | | CANVAS[H][W] = = '? ') {ICounter++; Pcounter++; if(Imaxline <icounter) {Imaxline=ICounter; } } Else{icounter=0; } } //except all of them? The situation if(pcounter!=0){ if(Imaxline >icounter) {Imaxline=ICounter; } } } /*if (Imaxw > Imaxh) {imaxtotal = Imaxh; } else{imaxtotal = Imaxw; } */ returnImaxline; } Public Static intSbpaint (Char[] [] Canvas) { intStamp = 1; intIMax =getmaxlength (Canvas); for(intSize = IMax; size>1; size-- ){ Char[] Tempcanvas =Canvas; for(intx=0; X+imax < Canvas.length; X + +){ for(inty=0; Y+imax < Canvas[x].length; y++){ if(Canbepaint (tempcanvas,x,y,size)) {Tempcanvas=Paint (tempcanvas,x,y,size); } } } if(Checkallpaint (Tempcanvas)) {Stamp=Size; Break; } } returnStamp; }
Train of thought 2: Directly find the horizontal and vertical continuous area, take its smallest one (do not know this right)
PS: This article in order to commemorate the two months of efforts, thanks to the son last night only a cry, let Dad sleep a good sleep.
[Software capability Test]: Silly x Stamp