Skip to content

Instantly share code, notes, and snippets.

@velicast
Last active December 18, 2015 15:09
Show Gist options
  • Save velicast/5802368 to your computer and use it in GitHub Desktop.
Save velicast/5802368 to your computer and use it in GitHub Desktop.
Dynamic Programming solution to problem http://codeforces.com/problemset/problem/22/B
#include <bits/stdc++.h>
using namespace std;
char b[28][28];
int dp[28][28], n, m, r;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> b[i][j];
b[i][j] = b[i][j]-'0' ? 0 : 1;
if (b[i][j]) dp[i][j] = dp[i][j-1]+1;
}
}
for (int i = n; i >= 1; --i) {
for (int j = m; j >= 1; --j) {
int w = 28, p = 0;
for (int h = 1; b[i-h+1][j]; ++h) {
w = min(w, dp[i-h+1][j]);
p = max(p, h+w);
}
r = max(r, p);
}
}
cout << 2*r << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment