http://acm.hdu.edu.cn/showproblem.php?pid=2686


双线程,嗯,好名字。

每次DFS传两个点的坐标,别让它们重合就行。

额外说一句,数据中有0


完整代码:

/*78ms,6112KB*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mx = 35;
const int dir[4][4] = {1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1};

int dp[mx][mx][mx][mx], a[mx][mx], N;

int f(int x1, int y1, int x2, int y2)
{
	if (a[x1][y1] == -1 || a[x2][y2] == -1) return 0;
	if (x1 == N && y1 == N) return a[N][N];
	if (dp[x1][y1][x2][y2] >= 0) return dp[x1][y1][x2][y2];
	if (x1 == x2 && y1 == y2 && x1 + y1 > 2) return 0;
	int ans = 0;
	for (int i = 0; i < 4; ++i)
		ans = max(ans, f(x1 + dir[i][0], y1 + dir[i][1], x2 + dir[i][2], y2 + dir[i][3]));
	return dp[x1][y1][x2][y2] = ans + a[x1][y1] + a[x2][y2];
}

int main()
{
	int i, j;
	while (~scanf("%d", &N))
	{
		memset(a, -1, sizeof(a));
		for (i = 1; i <= N; ++i)
			for (j = 1; j <= N; ++j)
				scanf("%d", &a[i][j]);
		memset(dp, -1, sizeof(dp));
		printf("%d\n", f(1, 1, 1, 1) - a[1][1]);
	}
	return 0;
}