cf 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 <cstdio>
- #include <cstring>
- #include <queue>
- #include <cmath>
- #include <vector>
- #include <algorithm>
- using namespace std;
- typedef long long ll;
- #define lowbit(x) x&(-x)
- #define max(x,y) (x>y?x:y)
- #define min(x,y) (x<y?x:y)
- #define mem(a) (memset(a,0,sizeof(a)))
- const int inf=0x3f3f3f3f;
- int main()
- {
- int n,m,ans=;
- cin>>n>>m;
- int imin=,jmin=;
- int imax=,jmax=;
- char a[n+][m+];
- for(int i=;i<=n;i++)
- {
- for(int j=;j<=m;j++)
- {
- cin>>a[i][j];
- if(a[i][j]=='B')
- {
- ans++;
- imin=min(imin,i);
- imax=max(imax,i);
- jmin=min(jmin,j);
- jmax=max(jmax,j);
- }
- }
- }
- int k=max(imax-imin,jmax-jmin)+;
- if(!ans) printf("1\n");
- else if(n>=k && m>=k)
- printf("%d\n",k*k-ans);
- else printf("-1\n");
- return ;
- }
cf B. Black Square的更多相关文章
- CF Theatre Square
Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard input ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- [CF 612E]Square Root of Permutation
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...
- CF 50E. Square Equation Roots
思路:这题的关键就是重复根只可能是整数. 这样先求出所有的根的数目,在减去重复的根. 代码如下: #include <iostream> #include <cstring> ...
- CF 711B - Chris and Magic Square
挺简单的一道题,但是做的时候没想好就开始写代码了,导致迷之WA,还是要多练习啊. #include <iostream> #include <cstdio> #include ...
- codeforce 1A Theatre Square
A. Theatre Square Theatre Square in the capital city of Berland has a rectangular shape with the siz ...
- 竞赛题解 - CF Round #524 Div.2
CF Round #524 Div.2 - 竞赛题解 不容易CF有一场下午的比赛,开心的和一个神犇一起报了名 被虐爆--前两题水过去,第三题卡了好久,第四题毫无头绪QwQ Codeforces 传送门 ...
- CF练习记录
2018/5/6 Codeforces Round #478 (Div. 2) C http://codeforces.com/contest/975/problem/C Valhalla Siege ...
- CF 234 C Weather(粗暴方法)
C. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- 【POJ 3714】Raid
[题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...
- ActiveMQ安装部署(Windows)
JMS(Java Messaging Service)是Java平台上有关面向消息中间件的技术规范,它便于消息系统中的Java应用程序进行消息交换,并且通过提供标准的产生.发送.接收消息的接口简化企业 ...
- SLF4j 和 common-logging
http://blog.csdn.net/xydds/article/details/51606010
- 洛谷 P1524 十字绣
P1524 十字绣 题目背景 考古学家发现了一块布,布上做有针线活,叫做“十字绣”,即交替地在布的两面穿线. 题目描述 布是一个n*m的网格,线只能在网格的顶点处才能从布的一面穿到另一面.每一段线都覆 ...
- 养活一款APP要“烧”多少钱?
Duang!又一款APP刷爆朋友圈.大片范儿的电影截图.意味深长的经典对白均出自一款名为“足记”的APP. 足记团队刚于去年8月完成天使期融资,投资方是光速创投和紫辉创投,目前正准备A轮融资.且近一周 ...
- Springmvc JSON交互
先上前端javascript.ajax代码 <pre name="code" class="javascript"> function testAj ...
- 蓝牙压力測试报抛android.os.TransactionTooLargeException异常分析总结
1.从main日志中找到异常点,例如以下: 08-20 11:05:19.754 5023 5023 E AndroidRuntime: FATAL EXCEPTION: main 08-20 11: ...
- IPA打包图片错误问题
CopyPNGFile /Users/gongihou/Library/Developer/Xcode/DerivedData/KTVgo-frborfduejxrajgpkfdaipygijow/B ...
- mysql-增删改数据
一.增加操作 INSERT用来插入或添加行到数据库表的.插入用以下几种方式: 1.插入完整的行 insert into customers values(null,'Pep E,LaPew','100 ...
- Visual Assist X 10.8.2036的Crack破解补丁.2014.05.22 (General release.)
说起来,VA公布上一个Genreal Release版本号已经是过春节那阵子时候的事了,时间过得真快. VA小组又给我们带来了新版本号的Visual Assist编码助手的 2036 版本号, 这个版 ...