Codeforce 22B Bargaining Table
B. Bargaining Table
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular roomN? ×?MMeters. each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and shoshould be placed so, that its sides are parallel to the office Wals. bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table shocould be initially free. bob wants the new table to sit as your people as possible, thus its perimeter shocould be maximal. help Bob find out the maximum possible perimeter of a bargaining table for his office.
Input
The first line contains 2 space-separated numbersNAndM(1? ≤?N,?M? ≤? 25)-the office room dimensions. Then there followNLinesMCharacters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. it's guaranteed that at least one square meter in the room is free.
Output
Output one number-the maximum possible perimeter of a bargaining table for Bob's office room.
Sample test (s) input
3 3000010000
Output
8
Input
5 411000000000000000000
Output
16
The deformation of HDU, is only changed from the area to the circumference of the specific analysis visible http://blog.csdn.net/iooden/article/details/38379065
#include
#include
#includeusing namespace std;const int N = 30;int l[N][N], r[N][N], h[N][N], n, m, ans;char a[N][N];int main(){ scanf ("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { h[i][0] = h[i][m + 1] = -1; scanf ("%s", a[i] + 1); for (int j = 1; j <= m; ++j) { if (a[i][j] == '0') h[i][j] = h[i - 1][j] + 1; l[i][j] = r[i][j] = j; } } int ans = 0; for (int i = 1; i <= n; ++i) { for (int j = m; j >= 1; --j) while (h[i][r[i][j] + 1] >= h[i][j]&&h[i][j]) r[i][j] = r[i][r[i][j] + 1]; for (int j = 1; j <= m; ++j) { while (h[i][l[i][j] - 1] >= h[i][j]&&h[i][j]) l[i][j] = l[i][l[i][j] - 1]; ans = max (ans, r[i][j] - l[i][j] + 1 + h[i][j]); } } printf ("%d\n", 2 * ans); return 0;}
In addition, the data for this question is relatively small. You can also use each point of the brute force enumeration as the upper left corner and then enumerate valid length and width to determine whether the matrix is composed of '0'. If yes, update the result.
#include
#include
const int maxn = 30;int n, m;int a[maxn][maxn];int sum[maxn][maxn];int main(){ while (~scanf ("%d%d", &n, &m)) { memset (a, 0, sizeof (a)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf ("%1d", &a[i][j]); memset (sum, 0, sizeof (sum)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + a[i][j]; } } int x, y; int ans = 0; for (int x1 = 1; x1 <= n; x1++) for (int y1 = 1; y1 <= m; y1++) for (int x2 = x1; x2 <= n; x2++) for (int y2 = y1; y2 <= m; y2++) { int tmp = sum[x2][y2] + sum[x1 - 1][y1 - 1] - sum[x1 - 1][y2] - sum[x2][y1 - 1]; if (tmp == 0) { int dx = x2 - x1 + 1; int dy = y2 - y1 + 1; if (ans < (dx + dy) * 2) ans = (dx + dy) * 2; } } printf ("%d\n", ans); } return 0;}