zoukankan      html  css  js  c++  java
  • CF475C. Kamal-ol-molk's Painting

    C. Kamal-ol-molk's Painting
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Rumors say that one of Kamal-ol-molk's paintings has been altered. A rectangular brush has been moved right and down on the painting.

    Consider the painting as a n × m rectangular grid. At the beginning an x × y rectangular brush is placed somewhere in the frame, with edges parallel to the frame, (1 ≤ x ≤ n, 1 ≤ y ≤ m). Then the brush is moved several times. Each time the brush is moved one unit right or down. The brush has been strictly inside the frame during the painting. The brush alters every cell it has covered at some moment.

    You have found one of the old Kamal-ol-molk's paintings. You want to know if it's possible that it has been altered in described manner. If yes, you also want to know minimum possible area of the brush.

    Input

    The first line of input contains two integers n and m, (1 ≤ n, m ≤ 1000), denoting the height and width of the painting.

    The next n lines contain the painting. Each line has m characters. Character 'X' denotes an altered cell, otherwise it's showed by '.'. There will be at least one altered cell in the painting.

    Output

    Print the minimum area of the brush in a line, if the painting is possibly altered, otherwise print  - 1.

    Sample test(s)
    input
    4 4
    XX..
    XX..
    XXXX
    XXXX
    output
    4
    input
    4 4
    ....
    .XXX
    .XXX
    ....
    output
    2
    input
    4 5
    XXXX.
    XXXX.
    .XX..
    .XX..
    output
    -1

    模拟即可,水题玩玩。代码能力略渣啊。。

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1000 + 10;
    char g[maxn][maxn];
    int n, m, minX, minY, ans;
    int sum[maxn][maxn], cnt, tot;
    int getSum(int a, int b, int x, int y) {
        return sum[a][b] - sum[a][y + 1] - sum[x + 1][b] + sum[x + 1][y + 1];
    }
    bool ok(int a, int b, int posX = minX, int posY = minY) {
        while(1) {
            if(posX + a - 1 > n || posY + b - 1 > m) return false;
            if(getSum(posX, posY, posX + a - 1, posY + b - 1) != a * b) return false;
            if(getSum(posX, posY, n, m) == a * b && getSum(posX, posY, posX + a - 1, posY + b - 1) == a * b) {
                return (cnt + a * b == tot) ;
            }
            bool flag1 = (a * b) == getSum(posX + 1, posY, posX + a, posY + b - 1);
            bool flag2 = (a * b) == getSum(posX, posY + 1, posX + a - 1, posY + b);
            if(!flag1 && !flag2) return false;
            if(flag1 && flag2) return false;
            if(flag1) ++posX, cnt += b;
            else ++posY, cnt += a;
        }
    }
    
    int main() {
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; ++i) {
            scanf("%s", g[i] + 1);
        }
        minX = n + 1, minY = m + 1;
        ans = n * m + 1;
        for(int i = n; 0 < i; --i) {
            for(int j = m; 0 < j; --j) {
                sum[i][j] = sum[i][j] - sum[i + 1][j + 1] + sum[i + 1][j] + sum[i][j + 1] + (g[i][j] == 'X');
                if(g[i][j] == 'X') {
                    minX = min(i, minX), minY = min(j, minY);
                    ++tot;
                }
            }
        }
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                if(ans <= i * j) continue;
                cnt = 0;
                if(ok(i, j)) ans = i * j;
            }
        }
        printf("%d
    ", ans == n * m + 1 ? -1 : ans);
    
        return 0;
    }

     

  • 相关阅读:
    笔试:一个逻辑题
    jmeter,学这些重点就可以了
    性能测试:通过一个案例(等待锁超时)告诉你,性能到底要不要熟悉业务逻辑?
    源码解读:webdriver client的原理 (面试自动化:如果你认为知道18种定位方式就算会自动化,那就太low了)
    测试必备:jmeter测试http协议接口的各种传参方式
    Vue笔记:封装 axios 为插件使用
    Vue笔记:使用 axios 发送请求
    Tomcat笔记:Tomcat的执行流程解析
    Git笔记:Git介绍和常用命令汇总
    Spring Boot使用Shiro实现登录授权认证
  • 原文地址:https://www.cnblogs.com/hzf-sbit/p/4013474.html
Copyright © 2011-2022 走看看