http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1231
Direct recursion.
Save the maximum number of paths to the maximum value at the same time, and note the first column of the first row.
Don't forget to take the mold.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5 Const intMAXN =510;6 intDP[MAXN][MAXN],P[MAXN][MAXN],Q[MAXN][MAXN];7 intMain ()8 {9 intN;Ten while(~SCANF ("%d",&N)) One { AMemset (DP,0,sizeof(DP)); - for(intI=1; i<=n;i++) { - for(intj=1; j<=n;j++) { thescanf"%d",&p[i][j]); -q[i][j]=1; - } - } + for(intI=1; i<=n;i++) { - for(intj=1; j<=n;j++) { + if(dp[i-1][j]>dp[i][j-1]) { Adp[i][j]=dp[i-1][j]+P[i][j]; at if(i==1|| j==1)Continue; -q[i][j]=q[i-1][j]; - } - Else if(dp[i][j-1]>dp[i-1][j]) { -dp[i][j]=dp[i][j-1]+P[i][j]; - if(i==1|| j==1)Continue; inq[i][j]=q[i][j-1]; - } to Else { +dp[i][j]=dp[i-1][j]+P[i][j]; - if(i==1|| j==1)Continue; theq[i][j]=q[i-1][j]+q[i][j-1]; * } $q[i][j]%=123456;Panax Notoginseng } - } the //for (int i=1;i<=n;i++) { + //for (int j=1;j<=n;j++) A //printf ("%d", q[i][j]); the //printf ("\ n"); + //} -printf"%d\n", q[n][n]%123456); $ } $ return 0; -}
View Code
XTU-1231 Life Achievement (DP + records the number of optimal solutions)