zoukankan      html  css  js  c++  java
  • 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
    5 4
    WWWW
    WWWB
    WWWB
    WWBB
    WWWW
    Output
    5

    Input 1 2 BB Output -1

    Input 3 3 WWW WWW WWW Output 1

    题意:

            给你一个广场,上面有黑色和白色的瓷砖,你可以把白色的瓷砖换成黑色的瓷砖,求最少要多少块黑色瓷砖来换才能构成一个黑色的正方形。 

    思路:

            找出两个黑色瓷砖的最远的距离即正方形的边长,如果边长大于原本广场的最小的边输出-1,要不然输出这个正方形所需的瓷砖总数-已有黑色瓷砖数。

    代码:

    #include<stdio.h>
    #include<iostream>
    using namespace std;
    int main()
    {
        char a[101][101];
        int m,n;
        cin>>m>>n;
        int sum=0;
        int maxx=0,maxy=0,minx=101,miny=101;
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                cin>>a[i][j];
        for(int i=0; i<m; i++)
            for(int j=0; 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+1>m||ans+1>n)
            printf("-1");
        else if(sum==0)
            printf("1");
        else
                printf("%d",(ans+1)*(ans+1)-sum);
        return 0;
    }
  • 相关阅读:
    通过枚举enum实现单例设计
    hibernate框架学习第二天:核心API、工具类、事务、查询、方言、主键生成策略等
    hibernate框架学习第一天:hibernate介绍及基本操作
    struts2框架之重复提交问题
    struts2框架之标签
    struts2框架之文件下载(参考第三天学习笔记)
    struts2框架之文件上传(参考第三天学习笔记)
    struts2框架之OGNL(参考第三天学习笔记)
    struts2框架学习之第三天
    struts2框架之拦截器(参考第二天学习笔记)
  • 原文地址:https://www.cnblogs.com/aiguona/p/7183241.html
Copyright © 2011-2022 走看看