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;
    }
    
  • 相关阅读:
    前端学习之路,前端开发人员如何在团队中规范git commit提交记录
    基于AntV图表库的Ant DeSign Charts图表展示的简单应用
    基于React-Amap组件库的高德地图简单应用
    高效的Coding,前端开发人员值得一看的前端开发工具
    解决git pull拉取更新代码失败,unable to resolve reference ‘refs/remotes/origin/xxx分支名’: reference broken问题
    position: sticky实现导航栏下滑吸顶效果
    javaScript保留三位有效数字
    封装属于自己的axios请求
    微博立场检测 60分Baseline
    Sequence to Sequence Learning with Neural Networks论文阅读
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319503.html
Copyright © 2011-2022 走看看