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 }
  • 相关阅读:
    redis 储存对象
    redis key 查看器
    c# 控制台程序编写RabbitMQ 生产者
    C# 使用Topshelf 构建 基于 window 服务的 RabbitMQ消费端
    asp.net webapi 使用定时任务Hangfire
    asp.net webpi 中使用 ClientHelper 发起HTTP请求
    SQL Server 导入和导出向导 未在本地计算机上注册Mircrosoft.ACE.OLEDB.12.0 提供程序
    c# 使用Linq 表达式 对查询结果分组,保留价格最低的一条
    Asp.Net s请求报传输流收到意外的 EOF 或 0 个字节
    asp.net webapi 中使用rdlc 报表
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7154397.html
Copyright © 2011-2022 走看看