DPL_3_A Largest Square

Calendar Clock iconCalendar Clock icon

AOJ

Table of contents

# Problem

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_3_A

# Time complexity

O(N2)O(N^2)

# Solution

// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int int
#define loop(x, start, end) for(Int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)
#define rep(n) for(int x = 0; x < n; x++)
#define span(a,x,y) a.begin()+x,a.begin()+y
#define span_all(a) a.begin(),a.end()
#define len(x) (x.size())
#define last(x) (*(x.end()-1))

using namespace std;
#define MAX_N 1401

Int H, W;
bool S[MAX_N][MAX_N];
Int A[MAX_N][MAX_N];

void input() {
  cin >> H >> W;
  loop(h,0,H) {
    loop(w,0,W) {
      cin >> S[h][w];
      A[h][w] = -1;
    }
  }
}

void solve() {
  Int max_ = 0;
  loop(h,0,H) {
    A[h][0] = (S[h][0]) ? 0 : 1;
    max_ |= A[h][0];
  }
  loop(w,0,W) {
    A[0][w] = (S[0][w]) ? 0 : 1;
    max_ |= A[0][w];
  }

  loop(h,1,H) {
    loop(w,1,W) {
      if (S[h][w]) {
        A[h][w] = 0;
        continue;
      }

      A[h][w] = min(A[h-1][w], min(A[h][w-1], A[h-1][w-1])) + 1;
      if (A[h][w] > max_) max_ = A[h][w];
    }
  }

  cout << max_*max_ << endl;
}

int main(void) {
  input();
  solve();
}

Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!

Offer jobs or contact me!

Comments