1 6 6 4 5 6 6 4 3 2 2 3 1 7 2 1 1 4 6 2 7 5 8 4 3 9 5 7 6 6 2 1 5 3 1 1 3 7 2
3948最简单的记忆化,没有什么好解释的,看代码就看懂了dp[x][y]代表x,y到终点的方案数#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int n,m,dp[105][105],a[105][105]; int check(int x,int y) { if(x<1 || x>n || y<1 || y>m) return 1; return 0; } int dfs(int x,int y) { if(dp[x][y]>=0) return dp[x][y]; dp[x][y] = 0; int i,j; for(i = 0; i<=a[x][y]; i++) for(j = 0; j<=a[x][y]-i; j++) { if(check(x+i,y+j)) continue; dp[x][y] = (dp[x][y]+dfs(x+i,y+j))%10000; } return dp[x][y]; } int main() { int t,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i = 1; i<=n; i++) for(j = 1; j<=m; j++) scanf("%d",&a[i][j]); memset(dp,-1,sizeof(dp)); dp[n][m] = 1; printf("%d\n",dfs(1,1)); } return 0; }
HDU1978:How many ways(记忆化),布布扣,bubuko.com
原文地址:http://blog.csdn.net/libin56842/article/details/38172729