java 代码
import java.util.*;
class Main{
static int n = 0, N = 15;
static int[][] nums = new int[N][N];
static int[][][][] max = new int[N][N][N][N];
public static void main(String[] args)throws Exception{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
while(true){
int a = sc.nextInt();
int b = sc.nextInt();
int w = sc.nextInt();
if(a == 0 && b == 0 && w == 0)break;
nums[a][b] = w;
}
for(int i1 = 1; i1 <= n; ++i1)
for(int j1 = 1; j1 <= n; ++j1)
for(int i2 = 1; i2 <= n; ++i2){
for(int j2 = 1; j2 <= n; ++j2){
int x = max[i1][j1][i2][j2];
if(i1 == i2 && j1 == j2){
x = Math.max(x, max[i1 - 1][j1][i2 - 1][j2] + nums[i1][j1]);
x = Math.max(x, max[i1 - 1][j1][i2][j2 - 1] + nums[i1][j1]);
x = Math.max(x, max[i1][j1 - 1][i2 - 1][j2] + nums[i1][j1]);
x = Math.max(x, max[i1][j1 - 1][i2][j2 - 1] + nums[i1][j1]);
}else{
x = Math.max(x, max[i1 - 1][j1][i2 - 1][j2] + nums[i1][j1] + nums[i2][j2]);
x = Math.max(x, max[i1 - 1][j1][i2][j2 - 1] + nums[i1][j1] + nums[i2][j2]);
x = Math.max(x, max[i1][j1 - 1][i2 - 1][j2] + nums[i1][j1] + nums[i2][j2]);
x = Math.max(x, max[i1][j1 - 1][i2][j2 - 1] + nums[i1][j1] + nums[i2][j2]);
}
max[i1][j1][i2][j2] = x;
}
}
System.out.print(max[n][n][n][n]);
}
}
java 代码
import java.util.*;
class Main{
static int n = 0, N = 15;
static int[][] nums = new int[N][N];
static int[][][] max = new int[N + N][N][N];
public static void main(String[] args)throws Exception{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
while(true){
int a = sc.nextInt();
int b = sc.nextInt();
int w = sc.nextInt();
if(a == 0 && b == 0 && w == 0)break;
nums[a][b] = w;
}
for(int k = 2; k <= n + n; ++k)
for(int i1 = 1; i1 <= n; ++i1)
for(int i2 = 1; i2 <= n; ++i2){
int j1 = k - i1, j2 = k - i2;
if(j1 >= 1 && j1 <= n && j2 >= 1 && j2 <= n){
int t = nums[i1][j1];
if(i1 != i2)t += nums[i2][j2];
int x = max[k][i1][i2];
x = Math.max(x, max[k - 1][i1 - 1][i2 - 1] + t);
x = Math.max(x, max[k - 1][i1][i2 - 1] + t);
x = Math.max(x, max[k - 1][i1 - 1][i2] + t);
x = Math.max(x, max[k - 1][i1][i2] + t);
max[k][i1][i2] = x;
}
}
System.out.print(max[n + n][n][n]);
}
}