Largest Allowed Area
时间限制: 1 Sec 内存限制: 128 MB
提交: 146 解决: 54
[提交] [状态] [命题人:admin]
题目描述
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
题目大意:
先输入一个整数t,代表有t组测试样例,对于每组测试样例,先输入两个整数n,m,然后输入n行m列的0,1矩阵,问在这个0,1矩阵中选取一个正方形区域,使得这个正方形区域内有且仅有一个1,问这样的正方形边长最大是多少。
解题思路:
可以通过二分正方形的边长,对于每次二分的mid,可以先预处理一下0,1矩阵,用sum[i][j]代表第i行第j列的元素的左上角的全部元素和,因此根据容斥思想可得出推导公式sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+arr[i][j],若一个正方形区域内仅包含一个1,则证明这个正方形区域的面积为1,因此对于每次二分出的正方形的边长,可以通过遍历一下这个矩阵,判断此边长是否会是的某个正方形区域的面积为1.
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int arr[1200][1200];
int sum[1200][1200];
int n,m;
bool judge(int x) {rep(i,1,n-x) {rep(j,1,m-x) {int t=sum[i+x][j+x]-sum[i+x][j-1]-sum[i-1][j+x]+sum[i-1][j-1];if(t==1) return true;}}return false;
}
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;scanf("%d",&t);while(t--) {scanf("%d %d",&n,&m);rep(i,1,n) {rep(j,1,m) {arr[i][j]=sum[i][j]=0;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=1,r=min(n,m);while(r-l>1) {int mid=(r+l)>>1;if(judge(mid)) l=mid;else r=mid;}printf("%d\n",l+1);}return 0;
}