• UVALive 4867 Maximum Square 贪心


    E - Maximum Square
    Time Limit:4500MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    Description

    Download as PDF

    Given an NxM matrix of all 1s and 0s, find the largest submatrix which is a square containing all 1s.

    Input

    There will be several test cases in the input. Each test case will begin with two integers, N and M(1$ le$N, M$ le$1, 000) indicating the number of rows and columns of the matrix. The next N lines will each contain M space-separated integers, guaranteed to be either 0 or 1. The input will end with a line with two 0s.

    Output

    For each test case, print a single integer, indicating the width (and height) of the largest square of all 1s, or 0 if there are no 1s. Print no extra spaces, and do not print any blank lines between answers.

    Sample Input

    4 5 
    0 1 0 1 1 
    1 1 1 1 1 
    0 1 1 1 0 
    1 1 1 1 1 
    3 4 
    1 1 1 1 
    1 1 1 1 
    1 1 1 1 
    6 6 
    0 0 0 0 0 0 
    0 0 0 0 0 0 
    0 0 0 0 0 0 
    0 0 0 0 0 0 
    0 0 0 0 0 0 
    0 0 0 0 0 0 
    0 0
    

    Sample Output

    3 
    3 
    0

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 1010
    const int inf=0x7fffffff;   //无限大
    int g[maxn][maxn];
    int main()
    {
        int m,n;
        while(cin>>n>>m)
        {
            if(m==0&&n==0)
                break;
            memset(g,0,sizeof(g));
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    cin>>g[i][j];
                }
            }
            int ans=0;
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(i==0||j==0)
                    {
                        ans=max(g[i][j],ans);
                        continue;
                    }
                    if(g[i][j]==0)
                        continue;
                    g[i][j]=min(g[i-1][j],min(g[i][j-1],g[i-1][j-1]))+1;
                    ans=max(g[i][j],ans);
                }
            }
    
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Java中的equals方法和==的区别
    C语言学习笔记--单向链表Markdown版本
    Tamias Blog's
    nginx图片服务器
    Nginx安装解决报错
    Detour框架注入样本无法正常启动(0x000007b)
    结构体指针中包含结构体指针
    Linux开发准备:Ubuntu14.04+Samba+MobaXterm+Source Insight 4.0
    在Visual Studio中将dll以资源的形式嵌入exe中
    MACD的价值不在于“金叉死叉”而在于背离
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4238196.html
Copyright © 2020-2023  润新知