#include<iostream> using namespace std; int map[21][21]; int d[21][21]; int N, M; int dp(int i, int j) { if(d[i][j]!=0) return d[i][j]; if(i==N-1&&j==M-1) return d[i][j]=map[i][j]; if(i==N-1&&j!=M-1) return d[i][j]=map[i][j] + dp(i,j+1); if(j==M-1&&i!=N-1) return d[i][j]=map[i][j] + dp(i+1,j); return d[i][j]=map[i][j]+(dp(i,j+1)>dp(i+1,j)?dp(i,j+1):dp(i+1,j)); } int main() { cin>>N>>M; for(int i=0; i<N; i++) for(int j=0; j<M; j++) cin>>map[i][j]; cout<<dp(0,0)<<endl; return 0; }
刚开始写错了,浪费好长时间错误是
d[i][j]=map[i][j]+(dp(i,j+1)>dp(i+1,j)?dp(i,j+1):dp(i+1,j));
被我写成了
d[i][j]=map[i][j]+dp(i,j+1)>dp(i+1,j)?dp(i,j+1):dp(i+1,j);