• 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 }
  • 相关阅读:
    【简】题解 AWSL090429 【市场】
    【简】题解 AWSL090429 【噪音】
    差分约束
    凸包模板
    杂模板
    后缀数组刷题
    Trie刷题
    字符串模板
    网络流建模专题
    组合数模板
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7154397.html
Copyright © 2020-2023  润新知