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 ...
随机推荐
- Camera Calibration 相机标定:原理简介(三)
3 绝对圆锥曲线 在进一步了解相机标定前,有必要了解绝对圆锥曲线(Absolute Conic)这一概念. 对于一个3D空间的点x,其投影空间的坐标为:x~=[x1,x2,x3,x4]T.我们定义无穷 ...
- ActiveMQ安装部署(Windows)
JMS(Java Messaging Service)是Java平台上有关面向消息中间件的技术规范,它便于消息系统中的Java应用程序进行消息交换,并且通过提供标准的产生.发送.接收消息的接口简化企业 ...
- Python Tkinter 基础控件学习
# -*- coding: utf-8 -*- from Tkinter import * def btn_click(): b2['text'] = 'clicked' evalue = e.get ...
- Hadoop框架基础(一)
** Hadoop框架基础(一) 学习一个新的东西,传统而言呢,总喜欢漫无目的的扯来扯去,比如扯扯发展史,扯扯作者是谁,而我认为这些东西对于刚开始接触,并以开发为目的学者是没有什么帮助的,反而 ...
- jsp登录页面 雏形
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【DNN 系列】 模块开发 8.0.1
1.创建第一个模块需要准备的东西有 https://github.com/dnnsoftware/DNN.Templates/releases/tag/1.0.1 VS 2015 插件 创建一个项目M ...
- 003.ES2015和ES2016新特性--类.md
JavaScript使用的是基于原型的OO模型,用对象字面量或者函数来实例化对象,用原型链来实现继承. 这样对于数据传统C++.Java的OO范式的开发者来说,会感到比较困惑,于是从ES2015开始逐 ...
- Python3基础笔记--生成器
目录: 一.列表生成器 二.生成器 三.迭代器 一.列表生成器 a = [x for x in range(10)] b= [y*2 for y in range(10)] def f(n) retu ...
- Linux-TCP/IP, IPv4地址类别摘要
TCP/IP分层: application layer transport layer internet ...
- redis 扩展 windows
下载地址:http://windows.php.net/downloads/pecl/releases/redis/