hdu 1208 Pascal's Travels

来源:互联网 发布:jk js 编辑:程序博客网 时间:2024/05/22 10:24

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1208

题意:从(0, 0)出发,到(n - 1, n - 1),当你在(r, c)的位置时,只能向右或者向下走Map(r,c)步,问有多少种走法。

DFS水题,注意一点细节就好

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxn 40using namespace std;int n;int Map[maxn][maxn];long long dp[maxn][maxn];int x[2] = {1, 0};int y[2] = {0, 1};void pre(){    memset(dp, -1, sizeof(dp));    dp[n - 1][n - 1] = 1;}long long DFS(int r, int c){    if(dp[r][c] != -1)    {        return dp[r][c];    }    long long ans = 0;    for(int i = 0; i < 2; i++)    {        int rr = r + x[i] * Map[r][c];        int cc = c + y[i] * Map[r][c];        if(rr < n && cc < n)        {            if((rr == n -1 && cc == n - 1)|| Map[rr][cc])// 第一个条件有点坑                ans += DFS(rr, cc);        }    }    return dp[r][c] = ans;}int main(){    while(~scanf("%d", &n), n != -1)    {        pre();        for(int i = 0; i < n; i++)        {            for(int j = 0; j < n; j++)            {                scanf("%1d", &Map[i][j]);//输入有点坑            }        }        cout << DFS(0, 0) << endl;    }    return 0;}