题目网址: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.

Input

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.

Output

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.

Examples
input
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
output
5
input
1 2
BB
output
-1
input
3 3
WWW
WWW
WWW
output
1

以第一个例子为例:

代码:
 #include <cstdio>
#include <algorithm>
using namespace std;
const int INF=;
int n,m;
int u,d,l,r;//上下左右取值
int a,b;//长宽
int num;//原始黑细胞数量
int len;//正方形边长
char square[][];
void init(){
u=l=INF;
d=r=-INF;
num=;
}
int main(){
init();
scanf("%d%d ",&n,&m);
for (int i=; i<n; i++) {
gets(square[i]);
for (int j=; j<m; j++) {
if(square[i][j]=='B'){
num++;
u=min(u, i);
d=max(d, i);
l=min(l, j);
r=max(r, j);
}
}
}
a=d-u+;
b=r-l+;
len=max(a, b);
if(num==) printf("1\n");
else if(n<len || m<len) printf("-1\n");
else printf("%d\n",len*len-num);
return ;
}
 

Codeforces Round #423 B. Black Square的更多相关文章

  1. Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)

    Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A.String Reconstruction B. High Load C ...

  2. Codeforces Round #423 (Div. 2)

    codeforces 423 A. Restaurant Tables [水题] //注意,一个人选座位的顺序,先去单人桌,没有则去空的双人桌,再没有则去有一个人坐着的双人桌.读清题意. #inclu ...

  3. 【Codeforces Round #423 (Div. 2) B】Black Square

    [Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...

  4. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

    Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It i ...

  5. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)

    题目链接:http://codeforces.com/contest/828 A. Restaurant Tables time limit per test 1 second memory limi ...

  6. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组

    E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...

  7. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load 构造

    D. High Load 题目连接: http://codeforces.com/contest/828/problem/D Description Arkady needs your help ag ...

  8. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集

    C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) A,B,C

    A.题目链接:http://codeforces.com/contest/828/problem/A 解题思路: 直接暴力模拟 #include<bits/stdc++.h> using ...

随机推荐

  1. 深度解析PHP数组函数array_slice

    看到array_slice()这个函数让我想起了VFP中的range这个范围取值的子句 这个函数一共有四个参数: 被取值的数组(必需) 取值的起始位置(必需) 取值的终止位置,如果不填写默认到数组最后 ...

  2. linux中的重要目录

    1./boot 引导程序,内核的存放的目录. 此目录,包含了在引导过程中所必须的文件,引导程序的相关文件(如:grub,lilo以及相应的配置文件及linux操作系统内核相关文件). 2./sbin/ ...

  3. 关于sqlmap的一些命令

    Options(选项):--version 显示程序的版本号并退出-h, --help 显示此帮助消息并退出-v VERBOSE 详细级别:0-6(默认为1)Target(目标):以下至少需要设置其中 ...

  4. php面向对象1

    类和对象的关系 首先看一段代码 var_dump()后的结果 注意 一个对象系统自动分配一个对象标识符!

  5. java基础之集合篇

    (一) 集合类位于JDK中java.util包中. 常用的集合类型分布在java.util.Collection和java.util.Map接口中. java.util.Collection=> ...

  6. C#基础知识-面向对象思想之继承(八)

    上一篇的标题编程思想我觉得不是很符合主题,因为编程思想的范围太大了,不仅仅是封装 继承 多态,所以比较符合主题的应该是面向对象思想.这一篇中将继续叙述面向对象思想中的继承. 从字面来看继承表达的意思已 ...

  7. ionic 中关于日期的转换格式

    //在HTML页面上{{ 2015-12-07T15:59:59.000Z | date }} //结果:December 7, 2015 {{ 2015-12-07T15:59:59.000Z | ...

  8. h5之scrollIntoView控制页面元素滚动

    如果滚动页面也是DOM没有解决的一个问题.为了解决这个问题,浏览器实现了一下方法,以方便开发人员如何更好的控制页面的滚动.在各种专有方法中,HTML5选择了scrollIntoView()作为标准方法 ...

  9. SSH连接不上CentOS 主机配置文件导致的原因的解决方法

    一.CentOS之SSH的安装与配置 SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定SSH 为建立在应用层和传输层基础上 ...

  10. 使用 XML 配置 MyBatis

    构建 SqlSessionFactory 最常见的方式是基于 XML 配置(的构造方式).下面的 mybatis-config.xml 展示了一个 典型的 MyBatis 配置文件的样子: XML C ...