【动态规划】Vijos P1143 三取方格数(NOIP2000提高组)

题目链接:

  https://vijos.org/p/1143

题目大意:

  NxN的矩阵,每个值只能取一次,从(1,1)走到(n,n)走三次能取得的最大值。

题目思路:

  【动态规划】

  f[x1][y1][x2][x3]表示第一次走x1,y1,相同步数下第二次走x2,y2,第三次走x3,y3的最大值。

  因为步数一样y2,y3可以直接求出来。

  

 

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define MAX 0x7f7f7f7f
26 #define PI 3.14159265358979323
27 #define N 24
28 using namespace std;
29 typedef long long LL;
30 int cas,cass;
31 int n,m,lll,ans;
32 int a[N][N];
33 int f[N][N][N][N];
34 int main()
35 {
36     #ifndef ONLINE_JUDGE
37 //    freopen("1.txt","r",stdin);
38 //    freopen("2.txt","w",stdout);
39     #endif
40     int i,j,x1,y1,x2,y2,x3,y3;
41 //    for(scanf("%d",&cas);cas;cas--)
42 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
43     while(~scanf("%d",&n))
44 //    while(~scanf("%d",&n))
45     {
46         for(i=1;i<=n;i++)
47         {
48             for(j=1;j<=n;j++)
49                 scanf("%d",&a[i][j]);
50         }
51         for(x1=1;x1<=n;x1++)
52         {
53             for(y1=1;y1<=n;y1++)
54             {
55                 for(x2=1;x2<=x1;x2++)
56                 {
57                     for(x3=1;x3<=x1;x3++)
58                     {
59                         y2=x1+y1-x2;y3=x1+y1-x3;
60                         f[x1][y1][x2][x3]=max(
61                         max(max(f[x1-1][y1][x2][x3],f[x1-1][y1][x2][x3-1]),
62                             max(f[x1-1][y1][x2-1][x3],f[x1-1][y1][x2-1][x3-1])),
63                         max(max(f[x1][y1-1][x2][x3],f[x1][y1-1][x2][x3-1]),
64                             max(f[x1][y1-1][x2-1][x3],f[x1][y1-1][x2-1][x3-1])))+a[x1][y1];
65                         if(x1!=x2 && y1!=y2)f[x1][y1][x2][x3]+=a[x2][y2];
66                         if(x1!=x2 && y1!=y2 && x1!=x3 && y1!=y3 && x2!=x3 && y2!=y3)f[x1][y1][x2][x3]+=a[x3][y3];
67                     }
68                 }
69             }
70         }
71         printf("%d\n",f[n][n][n][n]);
72     }
73     return 0;
74 }
75 /*
76 //
77 
78 //
79 */
View Code

  

posted @ 2016-08-16 20:18  Cool639zhu  阅读(408)  评论(0编辑  收藏  举报