Codeforces 828B Black Square(简单题)
Codeforces 828B Black Square(简单题)
Description
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.
Sample
Input WWWW
WWWB
WWWB
WWBB
WWWW
Output
5 Input BB
Output
-1 Input WWW
WWW
WWW
Output
题意:
给你一个广场,上面有黑色和白色的瓷砖,你可以把白色的瓷砖换成黑色的瓷砖,求最少要多少块黑色瓷砖来换才能构成一个黑色的正方形。
思路:
找出两个黑色瓷砖的最远的距离即正方形的边长,如果边长大于原本广场的最小的边输出-1,要不然输出这个正方形所需的瓷砖总数-已有黑色瓷砖数。
代码:
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
char a[][];
int m,n;
cin>>m>>n;
int sum=;
int maxx=,maxy=,minx=,miny=;
for(int i=; i<m; i++)
for(int j=; j<n; j++)
cin>>a[i][j];
for(int i=; i<m; i++)
for(int j=; j<n; j++)
{
if(a[i][j]=='B')
{
sum++;
if(i>maxx)
maxx=i;
if(i<minx)
minx=i;
if(j<miny)
miny=j;
if(j>maxy)
maxy=j;
}
}
int ans=max(maxx-minx,maxy-miny);
if(ans+>m||ans+>n)
printf("-1");
else if(sum==)
printf("");
else
printf("%d",(ans+)*(ans+)-sum);
return ;
}
Codeforces 828B Black Square(简单题)的更多相关文章
- Codeforces Round #599 (Div. 2)的简单题题解
难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...
- Codeforces/TopCoder/ProjectEuler/CodeChef 散题笔记 (持续更新)
最近做到了一些有趣的散题,于是开个Blog记录一下吧… (如果有人想做这些题的话还是不要看题解吧…) 2017-03-16 PE 202 Laserbeam 题意:有一个正三角形的镜子屋,光线从$C$ ...
- Codeforces#441 Div.2 四小题
Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- Bzoj2683 简单题
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1071 Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
随机推荐
- net core 程序docker打包镜像并发布到官方store
学习一个技术的第一步,总是要先打印或显示一个hello world的.当然,学习docker也不例外.上一篇文章已经简单的介绍了环境的安装和配置.接下来就要打印我们的hello world了. 首先我 ...
- 关于MATLAB处理大数据坐标文件2017527
第一次提交数据: 今天用了8个特征,加上的这一个特征是 从3000条测试数据中测试失败的数据总结出来的树的数目为50再次使用3000条测试数据测试结果-- 结果不错: 99%但是运行官网数据结果分数- ...
- mongodb入门笔记
mongodb作为nosql中排名第一的数据库,近年来使用的人数越来越多,作为开发人员,非常有必要了解下mongodb数据库.下面就给大家介绍下mongodb数据库的基本知识,有不对的地方欢迎指正,Q ...
- oracle11g的内存分配不当,导致的错误ORA-01034,ORA-00838,ORA-27101
由于开发需要,oracle11g被应用于虚拟机,所以系统资源显得弥足珍贵,百度了一下就有答案. 用dba身份进入Oracle,笔者在plsql中对数据库进行管理: show parameter sga ...
- 【Android Developers Training】 66. 添加动画
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- AngularJS模块
方式一: <body ng-app="myApp"> <div ng-controller="myCtrl1"> <h1>{ ...
- 记一次Nginx的配置
记第一次Nginx的配置 Nginx 首先了解到Nginx是干什么的?它有哪些作用?比较常用到的基础功能有反向代理.负载均衡.正向代理.http服务器.这次部署用到的就是反向代理. 反向代理就是指在目 ...
- angular.extend、angular.$watch、angular.bootstrap
1.angular.extend:依次将第二个参数及后续的参数的第一层属性(不管是简单属性还是对象)拷贝给第一个参数的第一层属性,即如果是对象,则是引用的是同一个对象,并返回第一个参数对象. 直接上代 ...
- javascript运动框架(三)
迟到了好几天,不好意思哈!继续来优化一下javascript运动框架的代码.之前的代码存在bug,当重复点击时速度会加快,那么怎么解决这个bug呢? 现在我们就来解决一下,其实很简单,在开始运动时,关 ...
- vijos1037题解
题目: 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念"9?11"事件,Mr.F决定自己用水晶来搭建一座双塔. Mr. ...