1 3 8 9 10 10 10 10 -10 10 10 10 -11 -1 0 2 11 10 -20 -11 -11 10 11 2 10 -10 -10
52
题意:从迷宫左上走到右下,取得最大值,有3种走法
思路:dp就好,但是WA了好几发,然后发现读错题了,注意一句话:但是如果向右走,则每次可以走一格或者走到该行的列数是当前所在列数倍数的格子。dp公式在代码里了。
#include <iostream> #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string> #include <string.h> #include <algorithm> #include <vector> #include <queue> #include <iomanip> #include <time.h> #include <set> #include <map> #include <stack> using namespace std; typedef long long LL; const int INF=0x7fffffff; const int MAX_N=10000; int T; int h,w,a; int dp[1009][1009]; bool cango(int x,int y){ return x>=1&&x<=h&&y>=1&&y<=w; } int main(){ cin>>T; while(T--){ cin>>h>>w; for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++){ scanf("%d",&a); dp[i][j]=-9999999; if(i==1&&j==1){ dp[i][j]=a; continue; } if(cango(i,j-1)){//从左到右挪1格 dp[i][j]=max(dp[i][j-1]+a,dp[i][j]); } for(int k=1;k<j;k++){//从左到右挪倍数格 if(cango(i,k)&&j%k==0){ dp[i][j]=max(dp[i][k]+a,dp[i][j]); } } if(cango(i-1,j)){//从上到下1格 dp[i][j]=max(dp[i-1][j]+a,dp[i][j]); } } } cout<<dp[h][w]<<endl; } return 0; }