B. Black Square(字符串)
1 second
256 megabytes
standard input
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.
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
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 <algorithm>
using namespace std;
typedef long long ll;
char a[][];
int main()
{
int n,m,ans1=;
int minx=,miny=,maxx=,maxy=,ans=;
cin>>n>>m;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]=='B')
{
ans++;
minx=min(i,minx);
miny=min(j,miny);
maxx=max(i,maxx);
maxy=max(j,maxy);
}
}
if(ans==) {puts("");return ;}
if(ans==) {puts("");return ;}
int xx=maxx-minx+,yy=maxy-miny+;
if(xx>m||yy>n) {puts("-1");return ;} ans=max(xx,yy)*max(xx,yy)-ans; cout<<ans<<endl;
return ;
}
B. Black Square(字符串)的更多相关文章
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- SQL-数学、字符串、时间日期函数和类型转换
--数学函数 --ABS绝对值,select ABS(-99)--ceiling取上限,select CEILING(4.5)--floor去下限select FLOOR(4.5)--power 几次 ...
- BestCoder Round #87 1002 Square Distance[DP 打印方案]
Square Distance Accepts: 73 Submissions: 598 Time Limit: 4000/2000 MS (Java/Others) Memory Limit ...
- sdut 2411:Pixel density(第三届山东省省赛原题,字符串处理)
Pixel density Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Pixels per inch (PPI) or pi ...
- 字符串核对之Boyer-Moore算法
算法说明: 在计算机科学里,Boyer-Moore字符串搜索算法是一种非常高效的字符串搜索算法.它由Bob Boyer和J Strother Moore设计于1977年.此算法仅对搜索目标字符串(关键 ...
- Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题
B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...
- C++ 字符串相关函数
<转>自:http://zhidao.baidu.com/question/173202165.html 首先就是memcpy表头文件: #include <string.h> ...
- php中的字符串和正则表达式
一.字符串类型的特点 1.PHP是弱类型语言,其他数据类型一般都可以直接应用于字符串函数操作. 1: <?php //输出345 //输出345 //先查找hello常量,若没找到,将hello ...
随机推荐
- BIRT-商务智能报表工具开发案例指南
BIRT 报表 脚本 开发 数据库 http://www.iteye.com/topic/1128694 打算近期出版一本全面介绍BIRT使用的书籍,能够帮助大家全面了解BIRT的方方面面,用丰富的案 ...
- [Luogu 1160] 队列安排
Luogu 1160 队列安排 链表H2O H2O H2O模板. 太久不写链表,忘干净了,竟调了半个晚上. 保留备用. #include <cstdio> #include <cst ...
- jenkins Process leaked file descriptors
https://stackoverflow.com/questions/17024441/process-leaked-file-descriptors-error-on-jenkins 1. BUI ...
- 深入理解Spring MVC(山东数漫江湖)
初始工程 使用Spring Boot和web,thymeleaf的starter来设置初始工程.xml配置如下: <parent> <groupId>org.springf ...
- JS对象操作
一.String常用操作 1.截取 substr(start,length) //返回从指定位置开始的指定长度的字符串. substring(start,end) //返回两个指定的位置之间的字符串. ...
- web_一些常用的线上脚本地址记录(个人使用)
1.jquery <script src="http://code.jquery.com/jquery-1.4.1.min.js"></script> 2. ...
- 上传漏洞新姿势(限Linux)
服务器:Linux当前环境:nginx/1.4.7PHP版本:PHP Version 7.0.0 上传情况简介:上传 111.jpg111 确实可以成功的但是上传 1.php.jpg1111.1 ...
- linux percpu机制解析【转】
转自:http://blog.csdn.net/wh8_2011/article/details/53138377 一.概述 每cpu变量是最简单也是最重要的同步技术.每cpu变量主要是数据结构数组, ...
- Redis缓存Mysql模拟用户登录Java实现实例[www]
Redis缓存Mysql模拟用户登录Java实现实例 https://jingyan.baidu.com/article/09ea3ede1dd0f0c0aede3938.html redis+mys ...
- Deep Learning基础--26种神经网络激活函数可视化
在神经网络中,激活函数决定来自给定输入集的节点的输出,其中非线性激活函数允许网络复制复杂的非线性行为.正如绝大多数神经网络借助某种形式的梯度下降进行优化,激活函数需要是可微分(或者至少是几乎完全可微分 ...