zoukankan      html  css  js  c++  java
  • B. Black Square(字符串)

    B. Black Square
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    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.

    Examples
    input
    5 4
    WWWW
    WWWB
    WWWB
    WWBB
    WWWW
    output
    5
    input
    1 2
    BB
    output
    -1
    input
    3 3
    WWW
    WWW
    WWW
    output
    1
    Note

    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.

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 typedef long long ll;
     5 char a[105][105];
     6 int main()
     7 {
     8     int n,m,ans1=0;
     9     int minx=200,miny=200,maxx=0,maxy=0,ans=0;
    10     cin>>n>>m;
    11     for(int i=1;i<=n;i++)
    12         for(int j=1;j<=m;j++)
    13         {
    14             cin>>a[i][j];
    15             if(a[i][j]=='B')
    16             {
    17                 ans++;
    18                 minx=min(i,minx);
    19                 miny=min(j,miny);
    20                 maxx=max(i,maxx);
    21                 maxy=max(j,maxy);
    22             }
    23         }
    24     if(ans==0) {puts("1");return 0;}
    25     if(ans==1) {puts("0");return 0;}
    26     int xx=maxx-minx+1,yy=maxy-miny+1;
    27     if(xx>m||yy>n) {puts("-1");return 0;}
    28 
    29     ans=max(xx,yy)*max(xx,yy)-ans;
    30 
    31     cout<<ans<<endl;
    32     return 0;
    33 }
  • 相关阅读:
    OSGI简介
    公司僵尸帐号引发了一系列的入侵事件-细说密码强度验证的重要性
    为其他对象提供一种代理以控制对这个对象的访问-代理模式
    运用共享技术有效地支持大量细粒度的对象-享元模式
    移动APP为什么要开发两套Android和IOS-桥接模式
    非法疫苗引发的思考 -外观模式
    你们还记得张江男、张江女两张图片吗?-装饰模式
    一个程序员的蜕变(我是如何成为架构师的)
    公司新加了一台友宝自动售货机引发的思考-适配器模式
    程序员如何应对北上广高房价示例解说-建造者模式
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7154397.html
Copyright © 2011-2022 走看看