hdu 1978 How many ways(dp)
如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点是(2,4)点,当他到达(2,4)点时将拥有1单位的能量,并开始下一次路径选择,直到到达(6,6)点。 我们的问题是机器人有多少种方式从起点走到终点。这可能是一个很大的数,输出的结果对10000取模。
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
方法一:当前的这个点可以到达其他点的方法数(直接4重循环)
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 10000
#define inf 1e12
#define N 106
int n,m;
int mp[N][N];
int dp[N][N];
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&mp[i][j]);
}
}
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
for(int k=i;(k<=n) && (k<=mp[i][j]+i);k++){
for(int w=j;(w<=m) && (w<=mp[i][j]+i+j-k);w++){
if((k==i) && (w==j))continue;
dp[k][w]+=dp[i][j];
dp[k][w]%=MOD;
}
}
}
}
printf("%d\n",dp[n][m]%MOD);
}
return ;
}
方法二:记忆化dp,标记dp[n][m]=1,然后从前往后记忆化dp,dfs
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 10000
#define N 106
#define inf 1e12
int n,m;
int mp[N][N];
int dp[N][N];
bool judge(int x,int y){
if(x< || x>n || y< || y>m) return false;
return true;
}
int dfs(int x,int y){
if(dp[x][y]>=) return dp[x][y];
dp[x][y]=;
for(int i=;i<=mp[x][y];i++){
for(int j=;j<=mp[x][y]-i;j++){
if(judge(x+i,y+j)){
dp[x][y]=(dp[x][y]+dfs(x+i,y+j))%MOD;
}
}
}
return dp[x][y];
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&mp[i][j]);
}
}
memset(dp,-,sizeof(dp));
dp[n][m]=;
printf("%d\n",dfs(,));
}
return ;
}
hdu 1978 How many ways(dp)的更多相关文章
- HDU 1978 How many ways(动态规划)
How many ways http://acm.hdu.edu.cn/showproblem.php?pid=1978 Problem Description 这是一个简单的生存游戏,你控制一个机器 ...
- HDU 2639 Bone Collector II (dp)
题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...
- HDU 1864 最大报销额(DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1864 题目: 最大报销额 Time Limit: 1000/1000 MS (Java/Others) ...
- HDU 4562 守护雅典娜(dp)
守护雅典娜 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submi ...
- HDU - 6199 gems gems gems (DP)
有n(2e4)个宝石两个人轮流从左侧取宝石,Alice先手,首轮取1个或2个宝石,如果上一轮取了k个宝石,则这一轮只能取k或k+1个宝石.一旦不能再取宝石就结束.双方都希望自己拿到的宝石数比对方尽可能 ...
- HDU 1978 How many ways(经典记忆化搜索)
S - How many ways Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU - 6357 Hills And Valleys(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=6357 题意 给一个数值范围为0-9的a数组,可以选择翻转一个区间,问非严格最长上升子序列,以及翻转的区间. 分析 ...
- 2014多校第四场1005 || HDU 4901 The Romantic Hero (DP)
题目链接 题意 :给你一个数列,让你从中挑选一些数组成集合S,挑另外一些数组成集合T,要求是S中的每一个数在原序列中的下标要小于T中每一个数在原序列中下标.S中所有数按位异或后的值要与T中所有的数按位 ...
- hdu 5623 KK's Number(dp)
问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗104)个数,每次KK都会先拿数.每 ...
随机推荐
- PHP 表单验证 - 验证 E-mail 和 URL
----------------------------------------------------------------------------- 本节展示如何验证名字.电邮和 URL. -- ...
- MSSQL 镜像
1.设置数据库CollectionDB 为完整备份模式服务端: USE master ALTER DATABASE CollectionGuest SET RECOVERY FULL GO 镜相端: ...
- EasyUI DataGrid定制默认属性名称
EasyUI DataGrid绑定服务器返回Json数据的解决方案 1. 服务器返回的数据对象格式,及初始化返回值 public class RequestResult { private int c ...
- Yougth的最大化(好题,二分查找 0 1分数规划)
Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价 ...
- 查看Linux操作系统版本
1.查看内核版本命令: [root@server1 Desktop]# cat /proc/version Linux version 2.6.32-358.el6.x86_64 (mockbui ...
- 关于vnc连接闪退问题的设置
设置如下: 依次点Option-->Advanced-->Expert找到ColourLevel,默认值是pal8,修改为rgb222或full.
- .Net中如何使用MySql连接池
提供一份官方的译文.翻译也挺辛苦的!! 6.4 Using Connector/Net with Connection Pooling 6.4在Connector/Net中使用连接池 The Conn ...
- X - A == B ?(第二季水)
Description Give you two numbers A and B, if A is equal to B, you should print "YES", or p ...
- bootstrap输入框从数据库读取数据
https://github.com/lzwme/bootstrap-suggest-plugin
- .Net Service部署(二)
1. 以管理员权限运行cmd.exe2. 安装服务 安装服务需要注意的是,如果电脑已经安装了此服务,需要先停止服务然后删除服务, 在进行安装注册,注册前先运行 cd C:\Windows\Micro ...