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 ...
 
随机推荐
- Spring定时任务实例
			
一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是出了名的任务调度框架,它可以与J2SE和J2EE应用程序相结合 ...
 - 关于cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题(不需要修改任何代码,只需要一个配置)
			
前言:今天在网上无意间看到cas单点登录排除请求的问题,发现很多人在讨论如何通过改写AuthenticationFilter类来实现忽略/排除请求URL的功能:突发奇想搜了一下,还真蛮多人都是这么干的 ...
 - javascript所有的节点和方法
			
属性: 1.Attributes 存储节点的属性列表(只读) 2.childNodes 存储节点的子节点列表(只读) 3.dataType 返回此节点的数据类型 4.Definition 以DTD或X ...
 - js函数一些小的知识点
			
var scope="123"; function aa(){ console.log(scope);//undefind var scope="234"; c ...
 - js返回顶部封装 简洁
			
js返回顶部封装 简洁: 加入html页面body最后面即可. <script> a(); function a() { $(function() { if ($(".j-to- ...
 - js返回格式化的日期(年-月-日)
			
var d = new Date(); var str = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate ...
 - 简析Android 兼容性测试框架CTS使用
			
一.什么是兼容性测试? 1)为用户提供最好的用户体验,让更多高质量的APP可以顺利的运行在此平台上 2)让程序员能为此平台写更多的高质量的应用程序 3)可以更好的利用Android应用市场 二.CTS ...
 - java CountDownLatch 使用介绍
			
CountDownLatch是在java1.5被引入的,跟它一起被引入的并发工具类还有CyclicBarrier.Semaphore.ConcurrentHashMap和BlockingQueue,它 ...
 - tomcat一个端口配置多个项目
			
在server.xml中增加host节点 <Host name="localhost" appBase="webapps" <!--这是默认的--- ...
 - 输入3个数a,b,c,按大小顺序输出
			
题目:输入3个数a,b,c,按大小顺序输出 package com.li.FiftyAlgorthm; import java.util.Scanner; /** * 题目:输入3个数a,b,c,按大 ...