两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
2020-2021:teams:bigbros:jerydeak:sps1 [2020/05/17 20:17] jerydeak |
2020-2021:teams:bigbros:jerydeak:sps1 [2020/05/17 20:21] (当前版本) jerydeak |
||
---|---|---|---|
行 14: | 行 14: | ||
<hidden Code> | <hidden Code> | ||
<codedoc code:c++> | <codedoc code:c++> | ||
- | #include<bits/c++.h> | + | #include<stdio.h> |
+ | #include<string.h> | ||
+ | #define condition(con,tr,fa) ((con)?(tr):(fa)) | ||
+ | #define max(a,b) condition(a>b,a,b) | ||
+ | #define min(a,b) condition(a<b,a,b) | ||
+ | #define reset(arr) memset(arr,0,sizeof(arr)) | ||
+ | |||
+ | int map[10][10]; | ||
+ | int ans[20][10][10]; | ||
+ | int ansflag[20][10][10]; | ||
+ | int n; | ||
+ | |||
+ | int ansf(int step,int x1,int x2){ | ||
+ | if(step==0)return map[0][0]; | ||
+ | if(ansflag[step][x1][x2]>0){ | ||
+ | return ans[step][x1][x2]; | ||
+ | } | ||
+ | int y1=step-x1; | ||
+ | int y2=step-x2; | ||
+ | |||
+ | int temp=0; | ||
+ | if(x1>0 && x2>0) temp=max(temp,ansf(step-1,x1-1,x2-1)); | ||
+ | if(x1>0 && y2>0) temp=max(temp,ansf(step-1,x1-1,x2)); | ||
+ | if(y1>0 && x2>0) temp=max(temp,ansf(step-1,x1,x2-1)); | ||
+ | if(y1>0 && y2>0) temp=max(temp,ansf(step-1,x1,x2)); | ||
+ | ans[step][x1][x2]=temp+map[x1][y1]+map[x2][y2]*condition(x1==x2,0,1); | ||
+ | ansflag[step][x1][x2]=1; | ||
+ | |||
+ | return ans[step][x1][x2]; | ||
+ | } | ||
int main(){ | int main(){ | ||
+ | reset(map); | ||
+ | reset(ans); | ||
+ | reset(ansflag); | ||
+ | |||
+ | scanf("%d",&n); | ||
+ | |||
+ | int x,y,val=1; | ||
+ | while(x!=0 || y!=0 || val!=0){ | ||
+ | scanf("%d%d%d",&x,&y,&val); | ||
+ | map[x-1][y-1]=val; | ||
+ | } | ||
+ | /* | ||
+ | printf("\n"); | ||
+ | for(int i=0;i<n;i++){ | ||
+ | for(int j=0;j<n;j++){ | ||
+ | printf("%d ",map[i][j]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | }printf("\n"); | ||
+ | */ | ||
+ | |||
+ | printf("%d",ansf(n*2-2,n-1,n-1)); | ||
} | } | ||
</codedoc> | </codedoc> | ||
行 33: | 行 84: | ||
***Solution:** 解类似1,不过在递归时,取消了 x1-1==x2-1 一项。另外,需要注意的是,多维数组不要忘了哪个分量为行或者列。 | ***Solution:** 解类似1,不过在递归时,取消了 x1-1==x2-1 一项。另外,需要注意的是,多维数组不要忘了哪个分量为行或者列。 | ||
+ | <hidden Code> | ||
+ | <codedoc code:c++> | ||
+ | #include<stdio.h> | ||
+ | #include<string.h> | ||
+ | #define condition(con,tr,fa) ((con)?(tr):(fa)) | ||
+ | #define max(a,b) condition(a>b,a,b) | ||
+ | #define min(a,b) condition(a<b,a,b) | ||
+ | #define reset(arr) memset(arr,0,sizeof(arr)) | ||
+ | #define MAXN 51 | ||
+ | |||
+ | int map[MAXN][MAXN]; | ||
+ | int ans[MAXN+MAXN][MAXN][MAXN]; | ||
+ | int ansflag[MAXN+MAXN][MAXN][MAXN]; | ||
+ | int m,n; | ||
+ | |||
+ | |||
+ | int ansf(int step,int x1,int x2){ | ||
+ | if(step==0)return map[0][0]; | ||
+ | if(ansflag[step][x1][x2]>0){ | ||
+ | return ans[step][x1][x2]; | ||
+ | } | ||
+ | int y1=step-x1; | ||
+ | int y2=step-x2; | ||
+ | |||
+ | int temp=0; | ||
+ | if(x1>0 && x2>0 && x1-1!=x2-1) temp=max(temp,ansf(step-1,x1-1,x2-1)); | ||
+ | if(x1>0 && y2>0 && x1-1!=x2) temp=max(temp,ansf(step-1,x1-1,x2)); | ||
+ | if(y1>0 && x2>0 && x1!=x2-1) temp=max(temp,ansf(step-1,x1,x2-1)); | ||
+ | if(y1>0 && y2>0 && x1!=x2) temp=max(temp,ansf(step-1,x1,x2)); | ||
+ | |||
+ | ans[step][x1][x2]=temp+map[y1][x1]+map[y2][x2];//*condition(x1==x2,0,1); | ||
+ | ansflag[step][x1][x2]=1; | ||
+ | |||
+ | return ans[step][x1][x2]; | ||
+ | } | ||
+ | |||
+ | int main(){ | ||
+ | reset(map); | ||
+ | reset(ans); | ||
+ | reset(ansflag); | ||
+ | |||
+ | scanf("%d%d",&m,&n); | ||
+ | |||
+ | int x,y,val=1; | ||
+ | for(int i=0;i<m;i++) | ||
+ | for(int j=0;j<n;j++) | ||
+ | scanf("%d",&map[i][j]); | ||
+ | printf("%d",ansf(m+n-2,n-1,n-1)); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | </codedoc> | ||
+ | </hidden> |