标签:
方法一:当前的这个点可以到达其他点的方法数(直接4重循环)
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 using namespace std; 15 #define ll long long 16 #define eps 1e-10 17 #define MOD 10000 18 #define inf 1e12 19 #define N 106 20 int n,m; 21 int mp[N][N]; 22 int dp[N][N]; 23 int main() 24 { 25 int t; 26 scanf("%d",&t); 27 while(t--){ 28 scanf("%d%d",&n,&m); 29 for(int i=1;i<=n;i++){ 30 for(int j=1;j<=m;j++){ 31 scanf("%d",&mp[i][j]); 32 } 33 } 34 memset(dp,0,sizeof(dp)); 35 dp[1][1]=1; 36 for(int i=1;i<=n;i++){ 37 for(int j=1;j<=m;j++){ 38 for(int k=i;(k<=n) && (k<=mp[i][j]+i);k++){ 39 for(int w=j;(w<=m) && (w<=mp[i][j]+i+j-k);w++){ 40 if((k==i) && (w==j))continue; 41 dp[k][w]+=dp[i][j]; 42 dp[k][w]%=MOD; 43 } 44 } 45 } 46 } 47 printf("%d\n",dp[n][m]%MOD); 48 } 49 return 0; 50 }
方法二:记忆化dp,标记dp[n][m]=1,然后从前往后记忆化dp,dfs
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 using namespace std; 15 #define ll long long 16 #define eps 1e-10 17 #define MOD 10000 18 #define N 106 19 #define inf 1e12 20 int n,m; 21 int mp[N][N]; 22 int dp[N][N]; 23 bool judge(int x,int y){ 24 if(x<1 || x>n || y<1 || y>m) return false; 25 return true; 26 } 27 int dfs(int x,int y){ 28 if(dp[x][y]>=0) return dp[x][y]; 29 dp[x][y]=0; 30 for(int i=0;i<=mp[x][y];i++){ 31 for(int j=0;j<=mp[x][y]-i;j++){ 32 if(judge(x+i,y+j)){ 33 dp[x][y]=(dp[x][y]+dfs(x+i,y+j))%MOD; 34 } 35 } 36 } 37 return dp[x][y]; 38 } 39 int main() 40 { 41 int t; 42 scanf("%d",&t); 43 while(t--){ 44 scanf("%d%d",&n,&m); 45 for(int i=1;i<=n;i++){ 46 for(int j=1;j<=m;j++){ 47 scanf("%d",&mp[i][j]); 48 } 49 } 50 memset(dp,-1,sizeof(dp)); 51 dp[n][m]=1; 52 printf("%d\n",dfs(1,1)); 53 } 54 return 0; 55 }
标签:
原文地址:http://www.cnblogs.com/UniqueColor/p/5152755.html