HDU 2859 Phalanx
大意:
给出一个矩阵,要求输出最大的 延左下到右上的对角线对称的 矩阵大小
思路:
(now[i][j])代表以((i,j))为左上角的长度为k矩阵是否对称,那么它可以由(pre[i-1][j])和(pre[i][j-1])转移过来,(pre[i][j])代表以((i,j))为左上角的长度为k-1的矩阵是否对称,所以如果(pre[i-1][j])和(pre[i][j-1])均为true,那么只需要(a[i][j]==a[i+k-1][j+k-1])即可
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
typedef long long LL;
int n, pre[N][N], now[N][N];
char a[N][N];
int main() {
while (cin >> n && n != 0) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
pre[i][j] = 1;
}
}
int res = 1;
for (int k = 2; k <= n; k++) {
int flag = 0;
for (int i = 0; i <= n - k; i++) {
for (int j = 0; j <= n - k; j++) {
if (a[i][j] == a[i + k - 1][j + k - 1] && pre[i + 1][j] &&
pre[i][j + 1])
now[i][j] = 1, flag = 1;
else
now[i][j] = 0;
}
}
if (flag)
res = k;
else
break;
for (int i = 0; i <= n - k; i++)
for (int j = 0; j <= n - k; j++) pre[i][j] = now[i][j];
}
cout << res << endl;
}
return 0;
}