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. 

Largest Allowed Area_第1张图片
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 
#include 
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<

 

你可能感兴趣的:(排序二分)