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.

样例输入

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

输出

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

题意:

给出n*m的矩阵 其中只包含0和1 要求在里面找出 边长最大的正方形 最多可以使用1个1

思路:

学长的思路 感觉很好

首先根据输入打好一张表:任意坐标位置 其左上部分包含该点上方和左方 全部点 1的总个数

例如示例1 打好表如下:

先枚举出 目标矩阵的边长 1 ~ min (n,m)

然后再用两层for循环 枚举x和y表示 矩阵左上角放置的位置 如果符合条件(即正方形内部1的个数<=1即可)直接跳出循环

因为枚举的边长是从大到小的 所以首先找到的一定是符合条件即最大的 代码如下:但T了

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e3;
int a[MAX+5][MAX+5],b[MAX+5][MAX+5];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&a[i][j]);
b[i][j]=b[i][j-1]+b[i-1][j]-b[i-1][j-1]+a[i][j];
}
}
/*
printf("\n\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
*/
int flag=0,flag1=0,ans=0;
for(int k=min(n,m);k>=0;k--){
//printf("%d ",k);
for(int i=0;i+k<n;i++){
for(int j=0;j+k<m;j++){
if(b[i][j]+b[i+k-1][j+k-1]-b[i-1][j+k-1]-b[i+k-1][j-1]<=1){
ans=k;
flag=1;
break;
}
}
if(flag==1){
flag1=1;
break;
}
}
if(flag1==1){
break;
}
}
printf("%d\n",ans);
}
return 0;
}

优化:

通过二分枚举的正方形边长方法降低复杂度 因为 当mid符合条件的时候 mid+1 ~ min(n,m)  也可能符合条件 反之 1 ~ mid-1 也可能是符合条件的

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e3;
int a[MAX+5][MAX+5],b[MAX+5][MAX+5];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&a[i][j]);
b[i][j]=b[i][j-1]+b[i-1][j]-b[i-1][j-1]+a[i][j];
}
}
int ans=0;
if(b[n-1][m-1]!=n*m){
int l=0,r=min(n-1,m-1);
while(l<=r){
int k=(l+r)/2,flag=0,flag1=0;
for(int i=0;i+k-1<n;i++){
for(int j=0;j+k-1<m;j++){
if(b[i-1][j-1]+b[i+k-1][j+k-1]-b[i-1][j+k-1]-b[i+k-1][j-1]<=1){
ans=k;
flag=1;
break;
}
}
if(flag==1){
flag1=1;
break;
}
}
if(flag1==1){
l=k+1;
}
else{
r=k-1;
}
}
}
printf("%d\n",ans);
}
return 0;
}

Largest Allowed Area【模拟+二分】的更多相关文章

  1. Gym 102091L Largest Allowed Area 【二分+二维前缀和】

    <题目链接> 题目大意:给你一个由01组成的矩形,现在问你,该矩形中,最多只含一个1的正方形的边长最长是多少. 解题分析: 用二维前缀和维护一下矩形的01值,便于后面直接$O(1)$查询任 ...

  2. Largest Rectangular Area in a Histogram

    题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚開始事实上没做这个题,而是在做https://oj.le ...

  3. Largest Rectangular Area in a Histogram 最大连续面积

    在HankerRank遇到一题计算柱状图连续矩形面积的问题. 举例 hist = [3, 2, 3]. 在这个柱状图里面最大可以容纳一个high = 2 length = 3的连续矩形, 其面积 = ...

  4. 【Leetcode_easy】812. Largest Triangle Area

    problem 812. Largest Triangle Area solution: class Solution { public: double largestTriangleArea(vec ...

  5. FZU 1575 小学生的游戏【模拟二分】

    某天,无聊的小斌叫上几个同学玩游戏,其中有比较笨的小兴,比较傻的小雪,可爱的小霞和自以为是的小楠.他们去找聪明的小明去给他们当裁判.判定谁取得游戏胜利. 而这个游戏是由小斌想个1到10000000的数 ...

  6. Hdu3640-I, zombie(模拟+二分)

    The "endless" model in "I, zombie" of "Plants vs. Zombies" is my favou ...

  7. [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  8. [LeetCode] Largest Triangle Area 最大的三角区域

    You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...

  9. LeetCode 812 Largest Triangle Area 解题报告

    题目要求 You have a list of points in the plane. Return the area of the largest triangle that can be for ...

随机推荐

  1. 云小课 | 搬迁本地数据至OBS,多种方式任你选

    摘要:搬迁本地数据至OBS,包括OBS工具方式.CDM方式.DES磁盘方式.DES Teleport方式和云专线方式,每种方式特点不同,本节课我们就一起看看有什么区别. 已有的业务数据可能保存在本地的 ...

  2. HTML5移动端最新兼容问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊.用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢?经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率来显示网 ...

  3. tp隐藏入口文件

    [ Apache ] httpd.conf配置文件中加载了mod_rewrite.so模块 AllowOverride None 将None改为 All 把下面的内容保存为.htaccess文件放到应 ...

  4. Cypress系列(2)- Cypress 框架的详细介绍

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html Cypress 简介 基于 JavaSc ...

  5. Flutter 使用Navigator进行局部跳转页面

    老孟导读:Navigator组件使用的频率不是很高,但在一些场景下非常适用,比如局部表单多页填写.底部导航一直存在,每个tab各自导航场景. Navigator 是管理路由的控件,通常情况下直接使用N ...

  6. unicode 的中文字符串,调用 isalnum()返回的是 True ?

    描述 Python isalnum() 方法检测字符串是否由字母和数字组成. 语法 isalnum()方法语法: str.isalnum() 返回值 如果 string 至少有一个字符并且所有字符都是 ...

  7. [Python基础]006.IO操作

    IO操作 输入输出 print raw_input input 文件 打开文件 关闭文件 读文件 写文件 文件指针 实例 输入输出 输入输出方法都是Python的内建函数,并且不需要导入任何的包就可以 ...

  8. 前端练手小项目——网页版qq音乐仿写

    qq音乐网页版仿写 一些步骤与注意事项 一开始肯定就是html+css布局和页面了,这段特别耗时间,耐心写完就好了 首先要说一下大致流程: 一定要先布局html!,所以一定要先分析页面布局情况,用不同 ...

  9. Java实现 蓝桥杯 算法训练 最小乘积

    算法训练 最小乘积(基本型) 时间限制:1.0s 内存限制:512.0MB 问题描述 给两组数,各n个. 请调整每组数的排列顺序,使得两组数据相同下标元素对应相乘,然后相加的和最小.要求程序输出这个最 ...

  10. Java实现 蓝桥杯VIP 基础练习 字符串对比

    问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...