HDU 1208 Pascal's Travels( 记忆化搜索)

题目大意:每一小格代表能向右或者向下走几步,问从左上走到右下总共有多少种走法。

dp[i][j]存放该格子有多少总走法。

#include <iostream>
#include <cstring>

using namespace std;

int n;
char a[40][40];
int s[40][40];
__int64 dp[40][40];

int X[]={1, 0};
int Y[]={0, 1};

__int64 dfs(int x, int y)
{
    if(dp[x][y] || !s[x][y])
        return dp[x][y];
    int xx, yy;
    for(int i=0; i<2; i++)
    {
        xx=x+X[i]*s[x][y];
        yy=y+Y[i]*s[x][y];
        if(xx>=0 && xx<n && yy>=0 && yy<n)
            dp[x][y]+=dfs(xx,yy);
    }
    return dp[x][y];
}

int main()
{
    while(cin>>n && n!=-1)
    {
        for(int i=0; i<n; i++)
            cin>>a[i];

        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                s[i][j]=a[i][j]-'0';

        memset(dp, 0, sizeof(dp));
        dp[n-1][n-1]=1;

        cout<<dfs(0,0)<<endl;
    }
    return 0;
}

HDU 1208 Pascal's Travels( 记忆化搜索)

时间: 2024-12-14 13:07:08

HDU 1208 Pascal's Travels( 记忆化搜索)的相关文章

HDU 4597 Play Game (记忆化搜索)

题意:有两堆n张的卡片,每张卡片有一个得分,Alice和Bob轮流在两堆卡片的两端取卡片 问Alice先手,取得分数最多为多少: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

hdu 1208 Pascal&#39;s Travels (子状态继承dp)

Pascal's Travels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1774    Accepted Submission(s): 781 Problem Description An n x n game board is populated with integers, one nonnegative integer

HDU 1078 FatMouse and Cheese(记忆化搜索DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意:一个n*n的图,每个点都有奶酪,老鼠从(0,0)开始走,每次最多只能走k步就要停下来,停下的这个位置的奶酪数只能比上一个停留的位置大,并获取其奶酪,每次只能水平或垂直走,问最多能得到的奶酪. 解题思路:记忆化搜索,这方面还是写的太少,还要看别人才会,这个就当个例子参考吧. 1 #include<cstdio> 2 #include<cstring> 3 #include

hdu 1142(迪杰斯特拉+记忆化搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7330    Accepted Submission(s): 2687 Problem Description Jimmy experiences a lot of stress at work these days, especiall

HDU 1428 漫步校园 (BFS + 记忆化搜索)

漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3360    Accepted Submission(s): 1009 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在电脑边,缺乏运动.他决定充分利用每次从寝室到机房的时间,在校园里散散步.整个HDU校园呈方形布局,可

hdu 1176免费馅饼(记忆化搜索)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 题意不解释了 简单的记忆化搜索可以拿来练练手,注意要从pos = 5 开始搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; const int M = 1e5

hdu 4111 Alice and Bob 记忆化搜索 博弈论

Alice and Bob Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4111 Description Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they

HDU 2476 String painter(记忆化搜索, DP)

题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B,那么我们DP区间的时候容易产生一个问题:假如我这个区间更新了,那么之前这个区间的子区间内DP出来的值就没用. 然后考虑到这里一直想不过去.最后看了看题解才知道. 我们可以先预处理一下怎么将一个空串变成B串需要的操作数. 这样我们就不用考虑子区间被覆盖的情况了. 如区间L,R 我们需要考虑的是点L是