HDOJ 2151 Worm

本文介绍了一个利用动态规划解决特定问题的方法,通过实例展示如何使用二维数组和循环来计算最优解。具体步骤包括初始化数组、迭代更新状态值以及最终输出结果。

http://acm.hdu.edu.cn/showproblem.php?pid=2151

 

 1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 int cost_mat[102][102];
5 int N,P,M,T;
6 int main()
7 {
8 while(scanf("%d%d%d%d",&N,&P,&M,&T)!=EOF){
9 int i,j;
10 memset(cost_mat,0,sizeof(cost_mat));
11 cost_mat[0][P]=1;
12 for(i=1;i<=M;i++)
13 for(j=1;j<=N;j++){
14 if(j-1>=1)
15 cost_mat[i][j]+=cost_mat[i-1][j-1];
16 if(j+1<=N)
17 cost_mat[i][j]+=cost_mat[i-1][j+1];
18 }
19 printf("%d\n",cost_mat[M][T]);
20 }
21 }



转载于:https://www.cnblogs.com/yangce/archive/2011/11/29/2268138.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值