hdu1978 How many ways
How many ways
Problem Description
这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下:
- 机器人一开始在棋盘的起始点并有起始点所标有的能量。
 - 机器人只能向右或者向下走,并且每走一步消耗一单位能量。
 - 机器人不能在原地停留。
 - 当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量。
 

如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点是(2,4),当他到达(2,4)点时将拥有1单位的能量,并开始下一次路径选择,直到到达(6,6)点。我们的问题是机器人有多少种方式从起点走到终点。这可能是一个很大的数,输出的结果对10000取模。
Input
第一行输入一个整数T,表示数据的组数。
对于每一组数据第一行输入两个整数n,m(1 <= n,m <= 100)。表示棋盘的大小。接下来输入n行,每行m个整数e(0 <= e < 20)。
Output
对于每一组数据输出方式总数对10000取模的结果.
Sample Input
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
Sample Output
3948
有点小水,只是我在写dp的方法的时候加少了判断条件,就调了一段时间
solution
1. 记忆化搜索
思路很简单,考虑到我们可以走到一个点然后就把能量用光的情况,所以总步数就是x+y的步数,然后思路就很简单
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int mod = 10000;
int dp[104][104],n,m,a[104][104];
int dfs(int x,int y){
    if(dp[x][y]>=0) return dp[x][y];
    dp[x][y]=0;
    for(int i=0;i<=a[x][y];i++)
        for(int j=0;j<=a[x][y]-i;j++){
            if(x+i>n||y+j>m)continue;
            dp[x][y]=(dfs(x+i,y+j)+dp[x][y])%mod;
        }
    return dp[x][y]%mod;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)for(int 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;
}
2.动态规划
记住要反向枚举,然后在记住不能自己加上自己就可以了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int mod = 10000;
int dp[104][104],n,m,a[104][104];
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		memset(dp,0,sizeof(dp));
		memset(a,0,sizeof(a));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				scanf("%d",&a[i][j]);
		dp[n][m]=1;
		for(int i=n;i>=1;i--)
			for(int j=m;j>=1;j--)
				for(int k=0;k<=a[i][j];k++)
					for(int h=0;h<=a[i][j]-k;h++){
						if(i+k>n||i+k<1||j+h>m||j+h<1||(k==0&&h==0))continue;
						dp[i][j]=(dp[i][j]+dp[i+k][j+h])%mod;
					}
		printf("%d\n",(dp[1][1])%mod);
	}
	return 0;
}
												
											hdu1978 How many ways的更多相关文章
- Hdu1978 How many ways                                                                                            2017-01-18 14:32             40人阅读              评论(0)              收藏
		
How many ways Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
 - [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
		
Given a string of numbers and operators, return all possible results from computing all the differen ...
 - [LeetCode] Decode Ways 解码方法
		
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
 - Decode Ways
		
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
 - 【LeetCode】241. Different Ways to Add Parentheses
		
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
 - [Leetcode] Decode Ways
		
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
 - Three ways to set specific DeviceFamily XAML Views in UWP
		
Three ways to set specific DeviceFamily XAML Views in UWP http://igrali.com/2015/08/02/three-ways-to ...
 - 241. Different Ways to Add Parentheses
		
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
 - Different Ways to Add Parentheses
		
Given a string of numbers and operators, return all possible results from computing all the differen ...
 
随机推荐
- HDU 4390 Number Sequence (容斥原理+组合计数)
			
HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a ...
 - Solaris 10 改ip參考文档之二
			
參考自:Cannot change IP address of Solaris[TM] 10 host (Doc ID 1007942.1) 本文不做翻译.全文转载. Symptoms Changin ...
 - HDU - 4758 Walk Through Squares (AC自己主动机+DP)
			
Description On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...
 - extjs grid 复制问题还有一种解决方式.
			
之前的项目中尽管也常常使用到extjs,但也许是没有注意到,也也许是根本就没有须要用到这个功能. 前几天在和客户讨论需求时,客户说想要可以将gird表中的数据复制出来,当时没多想,感觉这功能extjs ...
 - duang!!!为什么函数能够返回unique_ptr
			
C++虐我千百遍,我待C++如初恋 从智能指针说起 对高手而言.指针是上天入地的神器.对新手而言,那简直是灾难的源泉.高级语言如Java,C#都自己主动管理内存.你仅仅管new.不必担心内存释放问题. ...
 - 343D/Codeforces Round #200 (Div. 1) D. Water Tree  dfs序+数据结构
			
D. Water Tree Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each ...
 - Linux就该这么学 20181005(第七章磁盘管理)
			
参考链接https://www.linuxprobe.com lsblk检测硬盘使用情况 RAID 技术把多块物理硬盘设备(至少两块)通过硬件或软件的方式串联在一起,组成一个大的卷组,并将数据依次写入 ...
 - [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)
			
题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...
 - POJ 1952 DP
			
思路: 这题要求最长下降子序列的长度和个数,我们可以增加 数组maxlen[size](记录当前第1个点到第i个点之间的最长下降序列长度) 和maxnum[size](记录1~i之间的最长下降序列个数 ...
 - 752. [BJOI2006] 狼抓兔子
			
★★★ 输入文件:bjrabbit.in 输出文件:bjrabbit.out 简单对比时间限制:1 s 内存限制:162 MB Description Source: Beijin ...