B. Black Square
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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
Note

In the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).

In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.

In the third example all cells are colored white, so it's sufficient to color any cell black.

—————————————————————————————————

题目的意思是给出一张黑白的图,问把一些白色的点染成黑色使得所有黑色的点在一个正方形内,问最少染多少的白色点

思路:直接记录黑色点行和列的最大最小值,这样就能算出正方形最小值,然后判断行列数目是否达到这个正方形

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f; int m,n;
char mp[200][200];
int main()
{
scanf("%d%d",&m,&n);
int mni=INF,mnj=INF,mxi=-1,mxj=-1;
int cnt=0;
for(int i=0; i<m; i++)
{
scanf("%s",&mp[i]);
for(int j=0;j<n;j++)
{
if(mp[i][j]=='B')
{
mni=min(mni,i);
mnj=min(mnj,j);
mxi=max(mxi,i);
mxj=max(mxj,j);
cnt++;
}
}
}
if(mni==INF&&mnj==INF&&mxi==-1&&mxj==-1)
printf("1\n");
else if(mxi-mni>=mxj-mnj)
{
if(n<mxi-mni+1)
printf("-1\n");
else
printf("%d\n",(mxi-mni+1)*(mxi-mni+1)-cnt);
}
else
{
if(m<mxj-mnj+1)
printf("-1\n");
else
printf("%d\n",(mxj-mnj+1)*(mxj-mnj+1)-cnt);
}
return 0;
}

Codeforces828 B. Black Square的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  2. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  3. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  4. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  5. OPEN CASCADE Gauss Least Square

    OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...

  6. OpenCascade Eigenvalues and Eigenvectors of Square Matrix

    OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...

  7. Leetcode: Matchsticks to Square && Grammar: reverse an primative array

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  8. Leetcode: Valid Word Square

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  9. Modified Least Square Method and Ransan Method to Fit Circle from Data

    In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...

随机推荐

  1. gradle-4.1-all.zip离线包下载 极速 android studio2.3 3.0编译必备

    http://download.csdn.net/download/yongheng289/10039982 gradle-4.1-all.zip离线包下载 极速 android studio2.3 ...

  2. yum梳理

  3. OpenGL中的渲染方式—— GL_TRIANGLE_STRIP

    OpenGL值绘制三角形的方式常用的有三种,分别是GL_TRIANGLES.GL_TRIANGLE_STRIP.GL_TRIANGLE_FAN,其效果如依次是: 从左起:第一个方式是GL_TRIANG ...

  4. Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]

    今天在测试项目代码时,在idea中配置tomcat7插件运行后一直报如下错误: 解决方案:看了网上大多数办法都是修改xml文件配置,感觉并不适用,最后看到比较靠谱解释如下: pom.xml中jar包发 ...

  5. ReactiveX 学习笔记(20)使用 RxJava + RxBinding 进行 GUI 编程

    课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...

  6. IntelliJ IDEA2017 激活方法 最新的(亲测可用)

    IntelliJ IDEA2017 激活方法(亲测可用): 搭建自己的授权服务器,对大佬来说也很简单,我作为菜鸟就不说了,网上有教程. 我主要说第二种,现在,直接写入注册码,是不能成功激活的(如果你成 ...

  7. scrapy+redis去重实现增量抓取

    class ProjectnameDownloaderMiddleware(object): # Not all methods need to be defined. If a method is ...

  8. django + nginx + uwsgi

    server{ listen ; server_name 0.0.0.0; charset utf-; access_log /var/log/nginx/access.log main; locat ...

  9. WD Elements 与 time machine

    备份是很重要的问题. 之前买了一个 WD Elements,想要格式化成 HFS 的格式. 不然不能被 Time Machine 使用. 但是用磁盘工具不能成功,因为 EFI 分区的问题. 参考下面网 ...

  10. Python基础-python数据类型之集合(四)

    集合 集合是一个无序的,不重复的数据组合,基本功能包括关系测试和消除重复元素. 集合对象还支持 union,intersection,difference和 sysmmetric difference ...