zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Pipe
    An Easy Problem?!
    Kadj Squares
    Space Ant
    Intersection
    让网页变为可编辑状态
    typescript入门基础
    大家都能看懂的 canvas基础教程
    数组的foreach方法和jQuery中的each方法
    html单行、多行文本溢出隐藏
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4238196.html
Copyright © 2011-2022 走看看