我目前正在参加USACO 2006大赛的练习题,基本上如下:
给定点和X的网格,例如
...... ..X..X X..X.. ...... ..X...
找到面积最大的矩形,使其周长为 矩形不包含X.例如,矩形表示 f是最好的可能性。
.ffff. .fX.fX Xf.Xf. .ffff. ..X...
Here是最初的问题陈述。
到目前为止,我已经编写了一个代码,该代码在13个测试用例中有11个以正确的输出运行,但最后两个测试用例中的两个不起作用。这是我的代码。
#include <iostream>
using namespace std;
#define INF 0x3f3f3f3f
const int MAX = 200 + 5;
int N, M, MAXAREA = 0;
bool ok[MAX][MAX][MAX], land[MAX][MAX];
int main() {
/// read in input
// land[i][j] = 1 if there is a swampy area at (i, j)
cin >> N >> M;
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
char ch;
cin >> ch;
land[i][j] = (ch == 'X');
}
}
/// precompute crossing lines
// ok[col][row1][row2]: there are not X's from row1 to row2 in column col
for(int col = 0; col < M; col++)
for(int row1 = 0; row1 < N; row1++)
for(int row2 = row1; row2 < N; row2++) {
if(land[row2][col]) break;
else ok[col][row1][row2] = true;
}
/// run dynamic programming
// first loop through all pairs of rows
for(int row1 = 0; row1 < N; row1++) {
for(int row2 = row1; row2 < N; row2++) {
// for each pair of rows, go from left to right for columns. lhs_col = LHS column
for(int lhs_col = 0; lhs_col < M; ) {
// find the first LHS column that has no X's at all
if(!ok[lhs_col][row1][row2]) {
lhs_col++;
continue;
}
// next_col & last_col are next & last columns that are perfectly clear
int cur_col = lhs_col, next_col = INF, last_col = lhs_col;
// make sure cur_col has top and bottom vertices clear
while(cur_col < M && !land[row1][cur_col] && !land[row2][cur_col]) {
// finds the first and last column that are perfectly clear
if(ok[cur_col][row1][row2])
next_col = min(next_col, last_col = cur_col);
cur_col++;
}
// get maximum area
MAXAREA = max(MAXAREA, (row2 - row1 + 1) * (last_col - lhs_col + 1));
// if there is no next_col that is perfectly clear or the next one is itself
if(next_col == INF || next_col == lhs_col) break;
else lhs_col = next_col;
}
}
}
/// output answer
cout << MAXAREA << endl;
}
而here是导致其无效的输入案例之一。我的代码输出966,而它应该是1488.另一个测试用例是如此之大,没有必要在这里粘贴它。我非常感谢任何帮助。非常感谢你!