Largest Allowed Area 【前缀和+二分】

版权声明:本文为博主原创文章,转载请注明出处( • ̀ω•́ )✧ https://blog.csdn.net/wangws_sb/article/details/89674201

题目传送门

题目描述

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

题目大意

输入一个只有0 和1 的矩阵 ,现要求找一个边长尽可能长的正方形,正方形中至多只能有一个1,输出正方形的边长

解题思路

判断一个正方形区域内1的个数只需要计算这个正方形中的所有数的和,和的大小就是正方形中1的个数。可以用前缀和来求每个矩阵中1的个数, sum[i][j]表示从(1,1)到(i,j)的矩阵的所有元素和,然后二分去查找所有可能的边长m,判断是否会有边长等于m的符合题意的正方形。

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1100;
int t,n,m;
int arr[N][N],sum[N][N];
bool judge(int x)
{
    for(int i=1;i+x<=n;i++){
        for(int j=1;j+x<=m;j++){
            if(sum[i+x][j+x]+sum[i-1][j-1]-sum[i-1][j+x]-sum[i+x][j-1]<=1) return true;
        }
    }
    return false;
}
int main()
{
//    fin;
    scanf("%d",&t);
    while(t--)
    {
        memset(sum,0,sizeof(sum));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&arr[i][j]);
                sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+arr[i][j];
            }
        }
        int l=0;
        int r=min(n,m)+1;
        int mid;
        while(r-l>1){
            mid=(l+r)/2;
            if(judge(mid)) l=mid;
            else r=mid;
        }
        printf("%d\n",l+1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/89674201