Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1835 Accepted Submission(s): 1161
#include <stdio.h> int n,p,m,T; int abs(int x) //取绝对值函数 { return x>=0?x:-x; } int Dfs(int x,int t) { if(t==m) //t==m 返回,结束 { if(x==T) return 1; else return 0; } if(abs(T-x)>m-t||abs(T-x)%2!=(m-t)%2) //剪枝,排除 距离大于时间的情况和奇偶性不同时的情况 return 0; if(x==1) return Dfs(x+1,t+1); else if(x==n) return Dfs(x-1,t+1); else return Dfs(x-1,t+1)+Dfs(x+1,t+1); } int main() { int count; while(scanf("%d%d%d%d",&n,&p,&m,&T)!=EOF) { count=Dfs(p,0); printf("%d\n",count); } return 0; }
#include <stdio.h> #include <string.h> int main() { int i,j; int n,p,t,m; int dp[101][101]; while(scanf("%d%d%d%d",&n,&p,&m,&t)!=EOF) { memset(dp,0,sizeof(dp)); dp[p][0]=1; //初始化起始点 for(j=1;j<=m;j++) //也很关键,时间从1开始遍历到m for(i=1;i<=n;i++) dp[i][j]=dp[i-1][j-1]+dp[i+1][j-1]; printf("%d\n",dp[t][m]); } return 0; }