# acm_幸运大迷宫

Problem Description

Input

Output

Sample Input
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

Sample Output
52

#include <iostream>
#include <cstring>
using namespace std;
int inline max(int a,int b)
{
return a>b?a:b;
}
//int inline max2(int a,int b,int c)
//{
//    return max1(a,b)>c?max1(a,b):c;
//}
int main()
{
int i,m,n,C,c[21][1001],lucky[21][1001];
//freopen("r.txt","r",stdin);
cin>>C;
while(C!=0)
{
C=C-1;
cin>>m>>n;
for(i=1;i<=m;i++)
for(int j=1;j<=n;j++)
cin>>c[i][j];
memset(lucky,false,sizeof(lucky));
lucky[1][1]=c[1][1];
for(i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
for(int k=2;k<=1000;k++)
{
if(j*k<=n&&lucky[i][j*k]!=false) lucky[i][j*k]=max(lucky[i][k*j],lucky[i][j]+c[i][k*j]);
else if(j*k<=n&&lucky[i][k*j]==false) lucky[i][k*j]=lucky[i][j]+c[i][k*j];
else break;
}
if(i+1<=m&&lucky[i+1][j]!=false) lucky[i+1][j]=max(lucky[i+1][j],lucky[i][j]+c[i+1][j]);
if(i+1<=m&&lucky[i+1][j]==false) lucky[i+1][j]=lucky[i][j]+c[i+1][j];
if(j+1<=n&&lucky[i][j+1]!=false) lucky[i][j+1]=max(lucky[i][j+1],lucky[i][j]+c[i][j+1]);
if(j+1<=n&&lucky[i][j+1]==false) lucky[i][j+1]=lucky[i][j]+c[i][j+1];
}
cout<<lucky[m][n]<<endl;
}
return 0;
}