题目网址:http://codeforces.com/contest/828/problem/B
题目:
Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.
You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet.
The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.
Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.
5 4 WWWW WWWB WWWB WWBB WWWW
5
1 2 BB
-1
3 3 WWW WWW WWW
1 以第一个例子为例:
代码:
1 #include2 #include 3 using namespace std; 4 const int INF=1111111; 5 int n,m; 6 int u,d,l,r;//上下左右取值 7 int a,b;//长宽 8 int num;//原始黑细胞数量 9 int len;//正方形边长10 char square[105][105];11 void init(){12 u=l=INF;13 d=r=-INF;14 num=0;15 }16 int main(){17 init();18 scanf("%d%d ",&n,&m);19 for (int i=0; i