D-Small sunny teacher series--Sunny back gardenTime Limit:10000/5000ms (java/others) Memory Limit: 128000/64000kb (java/others) Submit Statusproblem Description
Small sunny day very beautiful back garden, after retiring after retirement in the inside of all kinds of flowers, raise grass, so now small sunny plan for his back garden fenced.
A small sunny backyard can be seen as a m*n rectangle, but some of them planted trees, these places can not install fences, now on a small sunny day to the back garden floor plan told you, please help design a largest rectangular fence.
Input
A single set of data, the first line consists of two integers m,n (1<=m,n<=500), followed by M lines, each line containing n characters, only by ' X ' and '. ' (ASCII codes are 120 and 46, respectively).
Where ' x ' means there are trees in this square, '. ' That means the fence can be built.
Output
If you can surround a closed fence, output an integer that indicates the maximum perimeter the fence can hit.
Otherwise output impossible
Sample Input
4 5......x.x ...... 2 2.xx.2 5.....xxxx.
Sample Output
14impossibleimpossible
Hint
Example one can circle the entire picture, the perimeter is (4+3) =14
Example two and three because of the existence of trees, a closed fence cannot be built.
This is entitled Single File read/write, sample includes three files
Test instructions
Enter M,n to indicate that there is a map of size m*n, and then enter that map, '. ' The open space, can build railings, ' x ' means trees, can not be built railings, asked you the map can be surrounded by the largest rectangular railings need how many railings, (the surrounding rectangle does not include trees, can only be composed of railings), in doing this problem is mainly to find the largest rectangle, do not rule how to do, then the mob search it, From each point as the starting point (the upper left point of the rectangle), but search also to search for a level.
First, find the maximum reachable point to the right of the starting point (x, y), set the maximum length to I, and then look down from (x,y+i), the point that can be reached, the maximum length is set to J, and then from (x+j,y+i) ~ (x+j,y) to determine whether it is '. ' Open space, if so, then from (x+j,y) ~ (x, y) to determine whether it is '. ' The open space, if any, indicates the ability to form a rectangle.
Record the maximum length of the rectangle I and J, the maximum number of railings Sum=max ((i+j) *2,sum);
finally in determining whether sum is 0, for 0 output impossible, otherwise output sum;
(PS: In each acquisition of I and J can be judged (i+m-1) * * OR (I+J) * * is greater than sum, if the sum is smaller, there is no need to continue to judge. )
#include <iostream>#include<stdio.h>#include<string.h>#defineMax (A, b) a>b?a:busing namespacestd;Charmap[1010][1010];intlen_x,len_y;/*maximum length and width of the map*/intSum/*maximum number of log rails*/intRet_y (intXintY/*Gets the maximum length (x, y) to the right*/{ intJ; for(j=1; j+y<=len_y;j++) {if(map[x][j+y]=='x') Break;} returnJ-1;}intRet_x (intXintY/*Gets the maximum length (x, y) down*/{ intJ; for(j=1; j+x<=len_x;j++) {if(map[x+j][y]=='x') Break;} returnJ-1;}voidWork (intXinty) { inti,j,k,l,ii,jj,kk,ll,sign1,sign2; //printf ("%d%d:\n", x, y); for(I=ret_y (x, y); i>=1; i--)/*Gets the (x, y) maximum right distance, and then decrements the length to find*/ { if(map[x][y+i]=='.'&& (i+len_x-1)*2>sum)/*judged for clearing and pruning*/ { for(J=ret_x (x,y+i); j>=1; j--)/*gets (x,y+i) the maximum downward distance, and then decrements the length of the lookup*/ { if(map[x+j][y+i]=='.'&& (I+J) *2>=sum)/*judged for clearing and pruning*/ { for(k=1, sign1=0; k<=i;k++)/*determine if the bottom of the rectangle is open space*/ { if(map[j+x][y+i-k]=='.') sign1++; Else Break; } if(sign1==i)/*The bottom of the rectangle meets the criteria*/ { for(k=1, sign2=0; k<j;k++)/*determine if the left side of the rectangle is empty*/ { if(map[j+x-k][y]=='.') sign2++; Else Break; } if(sign2==j-1)/*conforms to the constituent rectangle condition*/ { //printf ("\t%d%d\n", i,j);Sum=max (sum, (i+j) *2);/*gets the maximum value, in fact, each time gets is the maximum value*/ //return; } } } } } Else Break; } return ;}intMain () {inti,j; while(SCANF ("%d%d", &len_x,&len_y)! =EOF) {memset (Map,'x',sizeof(MAP));/*Set Fence*/ for(i=1; i<=len_x;i++) {scanf ("%s", map[i]+1); Map[i][len_y+1]='x';/*Set Fence*/ } for(i=1, sum=0; i<=len_x;i++) { for(j=1; j<=len_y;j++) { if(map[i][j]=='.') {work (I,J); /*Search every point*/ } } } if(sum) printf ("%d\n", sum); Elseprintf"impossible\n"); } return 0;}/*4 4..........x.. x.x*/
View Code
D-Small Sunny teacher series--Sunny back garden