zoukankan      html  css  js  c++  java
  • Largest Allowed Area

    A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, however, is finding the largest square region containing no forest. Unfortunately, there is no such region that is large enough for the headquarters they want to build. 
       
    After negotiation with the government and the evaluation of environmental impacts, the government allows the company to purchase land with at most one forest patch. In other words, the company’s goal is now finding the largest square region containing at most one forest patch. 
     
    To facilitate the search process, the company creates a map in the form of a 2D table consisting R rows and C columns. In this 2D table, each entry represents a land of patch where 0 corresponds to a non-forest patch and 1 to a forest patch. Unfortunately, the map may have up to 1,000 x 1,000 entries and it is not a good idea to manually look for the largest allowed square region. This is where your skill comes into play. Write an efficient algorithm to find such a square region. 

    输入

    The first line is a positive integer T <= 20 representing the number of test cases. For each case, the input is formatted as follows. 


    Note: there is at least one non-forest patch in each test case. 

    输出

    There are T lines in the output. Each line is the number of rows in the largest allowed square region for each case. 

    样例输入

    复制样例数据

    2 
    10 20 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1  
    0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    20 10 
    1 0 0 0 0 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0 1 0
    0 0 1 0 0 0 0 1 1 0
    0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 1 0 0 0
    0 0 0 0 0 0 0 0 0 0
    

    样例输出

    9
    7

    ps:二维前缀和。。。

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    int e[1111][1111],m,n;
    bool check(int x){
        for(int i=1;i<=n-x;i++){
            for(int j=1;j<=m-x;j++){
                if(e[i+x][j+x]+e[i-1][j-1]-e[i+x][j-1]-e[i-1][j+x]<=1) return true;
            }
        }
        return false;
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        int t;
        cin>>t;
        while(t--){
            cin>>n>>m;
            memset(e,0,sizeof e);
            int i,j;
            for(i=1;i<=n;i++){
                for(j=1;j<=m;j++){
                    cin>>e[i][j];
                    e[i][j]+=e[i-1][j]+e[i][j-1]-e[i-1][j-1];
                }
            }
            int l=0,r=min(n,m)+1;
            while(r-l>1){
                int mid=(l+r)/2;
                if(check(mid)) l=mid;
                else r=mid;
            }
            cout<<l+1<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    日期时间工具类
    jQuery 使用attr()方式设置 checked 失效原因及解决方法
    vue总结
    【笔记】golang中使用protocol buffers的底层库直接解码二进制数据
    【笔记】对golang的大量小对象的管理真的是无语了……
    【记录一个问题】thanos receiver在更换tsdb文件后,内存并未显著下降
    【笔记】论文阅读:《Gorilla: 一个快速, 可扩展的, 内存式时序数据库》
    【分享】thanos receiver的grafana报表配置
    【采坑小计】thanos receiver的官方文档中,并未说明tsdb落盘的配置方式
    【记录一个问题】thanos receiver在tsdb切换期间,导致remote write接口失败增加
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319503.html
Copyright © 2011-2022 走看看