http://acm.hdu.edu.cn/showproblem.php?pid=2571
dp。wa两次,设边从1开始,所以和0有关的除了ans[1][0]和ans[0][1]外都要初始化为无穷(不让从外面进来)
View Code
#include <stdio.h> int map[30][1100]; int ans[30][1100]; #define INF -100000000 int max(int a,int b) { return a>b?a:b; } int main() { int n,m; int t; int i,j,k; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&map[i][j]); for(i=0;i<=m;i++) ans[0][i]=INF; for(i=0;i<=n;i++) ans[i][0]=INF; ans[0][1]=ans[1][0]=0; for(i=1;i<=n;i++) for(j=1;j<=m;j++){ ans[i][j]=max(ans[i-1][j],ans[i][j-1])+map[i][j]; for(k=j-1;k>=1;k--) if(j%k==0) ans[i][j]=max(ans[i][j],ans[i][k]+map[i][j]); } printf("%d\n",ans[n][m]); } return 0; }