HDU 1208 跳格子题(很经典,可以有很多变形)

题目链接:

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

Pascal‘s Travels

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2795    Accepted Submission(s): 1298

Problem Description

An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from the upper left corner to the lower right corner of the board. The integer in any one square dictates how large a step away from that location must be. If the step size would advance travel off the game board, then a step in that particular direction is forbidden. All steps must be either to the right or toward the bottom. Note that a 0 is a dead end which prevents any further progress.

Consider the 4 x 4 board shown in Figure 1, where the solid circle identifies the start position and the dashed circle identifies the target. Figure 2 shows the three paths from the start to the target, with the irrelevant numbers in each removed.


Figure 1

Figure 2

Input

The input contains data for one to thirty boards, followed by a final line containing only the integer -1. The data for a board starts with a line containing a single positive integer n, 4 <= n <= 34, which is the number of rows in this board. This is followed by n rows of data. Each row contains n single digits, 0-9, with no spaces between them.

Output

The output consists of one line for each board, containing a single integer, which is the number of paths from the upper left corner to the lower right corner. There will be fewer than 2^63 paths for any board.

Sample Input

4

2331

1213

1231

3110

4

3332

1213

1232

2120

5

11101

01111

11111

11101

11101

-1

Sample Output

3

0

7

Hint

Hint

Brute force methods examining every path will likely exceed the allotted time limit.
64-bit integer values are available as "__int64" values using the Visual C/C++ or "long long" values
using GNU C/C++ or "int64" values using Free Pascal compilers.

Source

Mid-Central USA 2005

分析;

看到一句话:

所谓动态规划,就是每一个阶段你要做出一个决策,从最开始的决策到结束的决策的集合叫做一个策略,而各个阶段决策的多样性直接导致了策略的多样性,而我们一般求得的不是最有策略结束最大最小花费策略,每一个阶段的决策在这种有目的的作用下都与上一个阶段的决策相关,我们用记录这个阶段的一个东西(通常是动态数组)来记录这个状态符合我们需要的策略的决策

唉,还是不能很好的理解dp思想

本题分析;

要 求左上角到右下角的方法数

每次只能往下或者往右跳,跳的格子数必须等于现在站的格子的权值(不能少跳,比如你站的格子中权值是3,那么你在不超出边界的条件下你一定要跳3个格子),这样一来,有的格子可能跳不到,而有的格子可能有多种到达的方式

dp[i][j] 表示到达i,j的方法数目

注意:

1.初始化问题:dp初始化为0,dp[0][0]=1

2.状态转移方程:

dp[i][j+a[i][j]]+=dp[i][j];

dp[i+a[i][j]][j]+=dp[i][j];

不超边界的前提下(j+a[i][j]<n   i+a[i][j]<n)

3.long long 类型(注意题目中拿到2的63次方)

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define max_v 40
long long dp[max_v][max_v];//dp[i][j] 表示到达i行j列有多少种走法
long long a[max_v][max_v];
char s[max_v];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==-1)
            break;
        getchar();
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            for(int j=0;j<strlen(s);j++)
            {
                a[i][j]=s[j]-‘0‘;
            }
        }
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(a[i][j]==0)//防止死循环
                    continue;
                if(j+a[i][j]<n)//向右边跳
                {
                    dp[i][j+a[i][j]]+=dp[i][j];
                }
                if(i+a[i][j]<n)//向下跳
                {
                    dp[i+a[i][j]][j]+=dp[i][j];
                }
            }
        }
        printf("%lld\n",dp[n-1][n-1]);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/yinbiao/p/9158839.html

时间: 2024-08-28 02:34:24

HDU 1208 跳格子题(很经典,可以有很多变形)的相关文章

HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27185    Accepted Submission(s): 6630 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

hdu 4828 Xor Sum (trie 树模板题,经典应用)

hdu 4825 题目链接 题意:给定n个数,然后给出m个询问,每组询问一个数x,问n中的数y使得x和y的异或和最大. 思路:字典树..把每个数转化成二进制,注意补全前导0,使得所有数都有相同的位数. 如果想要异或和最大,那么每一位尽可能都是1. 所以做法是,先构建字典树,然后每次find的时候,尽可能按照和当前寻找的数的位相反的位的方向走(如果有的话) 比如当前位是1,那我就往0的方向走. 需要注意的是,多组数据,每次要重新初始化一遍. 做法是 在struct 中重新 root = new N

poj 1006:Biorhythms(水题,经典题,中国剩余定理)

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical,

HDU 2553 N皇后问题 --- 经典回溯

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2553 DFS+回溯 /* HDU 2553 N皇后问题 --- 经典回溯 */ #include <cstdio> #include <cstring> const int maxn = 15; int cnt, n; bool visit[3][maxn*2];//3个数组分别标记列,y+x斜线,y-x斜线是否冲突 注意要*2 /* 从第r行开始满足条件地放置皇后 r表示行(从0开

hdu 1254(搜索题)

推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7062    Accepted Submission(s): 2009 Problem Description 推 箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工 只能推箱子而不能拉箱

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题

Spring3与Hibernate4整合时出现了nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider. hibernate3的时候,用spring来控制sessionfactory用的可以是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,因为用的是hibernate4所以照猫画

很经典的mysql的幻读解释

http://blog.sina.com.cn/s/blog_499740cb0100ugs7.html 上述链接很经典的解释了mysql的mvcc为什么是部分解决了幻读的问题. 同时我需要理解的是,在业务逻辑中,事务是可以分散在业务代码里面的,并不是说一条语句写出.比如,我们启动了一个事务,start transaction.先获取数据库中的数据,然后在业务中判断该条件是否符合自己的业务逻辑,如果是的话,那么就可以插入一部分数据.但是这个时候可能也有别的数据插入进来了,产生了冲突,导致当前的数

hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。

题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有的连通分量只有一个点,当舍去该点时候,连通分量-1: 复习求割点的好题! #include<iostream> #include<cstdio> #include<vector> using namespace std; int n,m; vector<vector&