http://poj.org/problem?id=2078
枚举每行位移多少次,即可。
#include <stdio.h>
#include <algorithm>
const int MAXN = 8;
const int INF = 0x3f3f3f3f;
int matrix[MAXN][MAXN];
int shift[MAXN];
int dfs(int i, int n)
{
if (i == n) {
int res = -INF;
for (int j = 0; j < n; j++) {
int sum = 0;
for (int i = 0; i < n; i++)
sum += matrix[i][(j + shift[i]) % n];
res = std::max(res, sum);
}
return res;
}
int res = INF;
for (shift[i] = 0; shift[i] < n; shift[i]++) {
res = std::min(res, dfs(i + 1, n));
}
return res;
}
int main()
{
int n;
for (; scanf("%d", &n) == 1, n != -1; ) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &matrix[i][j]);
printf("%d\n", dfs(1, n));
}
return 0;
}