Test instructions: There's a n*m big lawn, '. ' The open space, ' x ' means planting a tree, now to build a rectangular fence for the lawn, the fence must be built in the open space, ask the fence to build the maximum perimeter is how much, that is, the maximum number of space occupied.
Focus: Violence, the need to preprocess the number of open space on the right of each open space to r[i][j] and the number of empty space d[i][j], and then enumerate each open space as the top left corner of the fence vertex, according to R and D two arrays to get up and left two edges possible length, Then enumerate the upper and left lengths if both the bottom and right conditions are met (that is, do not appear ' X ') to update the maximum value.
#include <stdio.h>#include <string.h>#include <algorithm>using namespace STD;Const intN =505;intN, M;CharG[n][n], R[n][n], d[n][n];intMain () { while(scanf("%d%d", &n, &m) = =2) {memset(R,0,sizeof(r));memset(d,0,sizeof(d)); for(inti =0; I < n; i++)scanf('%s ', G[i]); for(inti =0; I < n; i++) for(intj =0; J < M; J + +) {if(G[i][j] = ='. ') { for(intK = j +1; K < M && G[i][k]! =' x '; k++) r[i][j]++; for(intK = i +1; K < n && g[k][j]! =' x '; k++) d[i][j]++; } }intres =0; for(inti =0; I < n; i++) for(intj =0; J < M; J + +) {if(G[i][j] = ='. ') { for(intR1 = R[i][j]; R1 >=1; r1--) for(intD1 = D[i][j]; D1 >=1; d1--)if(R[i + d1][j] >= R1 && d[i][j + r1] >= d1) res = max (res,2* (r1 + d1)); } }if(Res >=4)printf("%d\n", res);Else printf("impossible\n"); }return 0;}
Acdream 1705 (violence)