题目网址: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. 测评:华为最新移动应用/APP测试工具MobileTest

    一.目前移动应用/App的测试痛点及可选方案 移动互联网市场进入下半场,同质化竞争激烈,平均获客成本增加.屏幕不适配.闪退.无响应.UI异常等兼容性问题严重影响用户体验,影响用户转化率和用户粘性.如何 ...

  2. VR全景智慧城市-提前进入商家观景,涵盖实体行业

    互联网发展的迅猛势头,让很多的实体商家翻了个大跟头,更有言说,未来的大街小巷根本见不到逛街的人,可是线上商城相继曝出的假货谁来买单?相比之下眼见为实更让消费者心里觉得踏实.所以,全景智慧城市更能满足大 ...

  3. QUICK-AP + BETTERCAP 替换局域网内其他用户的下载文件为自定义文件

    环境需求 :kali系统 , .0版本 :quick-ap :bettercap :bettercap-proxy-modules :...... 主要环境搭建 目的:替换局域网用户的下载文件,变为我 ...

  4. 在linux下利用信号量实现一个写者线程多个读者线程

    #include<pthread.h> #include<string.h> #include<stdlib.h> #include<stdio.h> ...

  5. 用Java写的简单五子棋游戏(原创五子连珠算法)

    源码jar包(已安装jdk环境可直接运行) 下载地址:http://download.csdn.net/detail/eguid_1/9532912 五子连珠算法为自创算法,对于五子棋该算法性能足以. ...

  6. DDD理论学习系列(6)-- 实体

    DDD理论学习系列--案例及目录 1.引言 实体对应的英语单词为Entity.提到实体,你可能立马就想到了代码中定义的实体类.在使用一些ORM框架时,比如Entity Framework,实体作为直接 ...

  7. php实现ppt转图片,php调用com组件问题

    PHP 调用com组件将ppt转为图片. 需要在php.ini中开启 extension=php_com_dotnet.dllcom.allow_dcom = true   测试代码如下:   < ...

  8. JQuery 实现返回顶部

    1.添加html <div id="back-to-top"> <a href="javascript:;" title="返回顶部 ...

  9. asp net core 跨平台初体验

    标: 在 ubuntu 16.04 上部署一个 asp.net core 站点,打开网站后显示一段文字.   安装 net core 运行环境:ubuntu 16.04 LTS 1.添加 apt 源 ...

  10. ubuntu eclipse 建立server 提示coud not load the tomcat server configuration at /opt/apache ...的解决方法

    ubuntu eclipse 建立server 提示coud not load the tomcat server configuration at /opt/apache ...的解决方法 & ...