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 ...
随机推荐
- ecshop调用指定分类和个数的文章列表
举例如首页调用方法: 1.先打开index.php文件找到以下代码: $smarty->assign('new_articles', index_get_new_articles()); // ...
- Kafka官方文档翻译——实现
IMPLEMENTATION 1. API Design Producer APIs Producer API封装了底层两个Producer: kafka.producer.SyncProducer ...
- bc计算A股上市新股依次涨停股价
几年的股市可谓惨不忍睹,不提也罢.唯有打新中签的时候,心里稍微有那么一点点的补偿,于是内心就YY可以30板吗,可以40板吗.于是就写了个连板的bc程序,每次中签的时候就运行一下,然后尽情的YY,然而每 ...
- Elasticsearch5.0.1安装
最新研究了下ES5.0,ES就是为高可用和可扩展而生的,你可以很方便的增加也减少一个节点.顺便记录下安装过程,也方便以后查看. 1 安装部骤 1.1 安装JDK ES依赖于 ...
- EventBus In eShop -- 解析微软微服务架构Demo(四)
引言 大家好像对分析源码厌倦了,说实在我也会厌倦,不过不看是无法分析其后面的东西,从易到难是一个必要的过程. 今天说下EventBus,前几天园里的大神已经把其解刨,我今天就借着大神的肩膀,分析下在e ...
- 9.session的生命周期
1.创建 当客户端第一次访问某个jsp或者Servlet的时候,服务器会为当前会话创建一个SessionId,每次客户端向服务端发送请求的时候,都会将此SessionId携带过去,服务端会对此Sess ...
- 从栈不平衡问题 理解 calling convention
最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...
- ASP搜索查询
html code: <form name="frm_Search" method="get" action="Search.asp" ...
- Vijos 1004 伊甸园日历游戏 博弈
描述 Adam和Eve玩一个游戏,他们先从1900.1.1到2001.11.4这个日期之间随意抽取一个日期出来.然后他们轮流对这个日期进行操作: 1 : 把日期的天数加1,例如1900.1.1变到19 ...
- shell十分钟教程
1.先介绍下shell的工作原理 Shell可以被称作是脚本语言,因为它本身是不需要编译的,而是通过解释器解释之后再编译执行,和传统语言相比多了解释的过程所以效率会略差于传统的直接编译的语言. 但是s ...