uva825 - Walking on the Safe Side(动规)

动规, 记忆化搜索。。。。

思路不难

状态:dp[i][j]表示点(I,j)到终点的最短路径数目;

状态转移:dp[i][j] = dp[i+1][j]+dp[i][j+1];

还有就是这道题的测试数据很操蛋,两个数之间不是规律的有一个空格,有可能有多个空格,我卡到这一点上wa了好几次。

所以说读取的时候要用字符串读取。

代码如下;

#include <cstdio>
#include <cstring>
#define M 105
#define N 105
int m, n, a[N][N], d[N][N];
char s[N];
int dp(int x, int y)
{
    int &ans = d[x][y];
    if(ans) return ans;
    if(a[x][y]) return ans = 0;
    if(x==m&&y==n) return ans = 1;
    ans = 0;
    int nx = x+1, ny = y+1;
    if(nx<=m) ans+=dp(nx,y);
    if(ny<=n) ans+=dp(x,ny);
    return ans;
}
int main ()
{

    int cas, x, t, tt = 0;
    char ch;
    scanf("%d",&cas);
    while(cas--)
    {
        memset(a,0,sizeof(a));
        memset(d,0,sizeof(d));
        scanf("%d%d",&m,&n);
        for(int i = 1; i <= m; i++)
        {
            scanf("%d",&x);
            gets(s);
            int len = strlen(s);
            t = 0;
            for(int j = 0; j <= len; j++)
            {
                if(s[j]>='0'&&s[j]<='9') t = t*10+s[j]-'0';
                else { a[x][t] = 1; t = 0; }
            }
        }

        if(tt++) printf("\n");
        printf("%d\n",dp(1,1));
    }
    return 0;
}


 

 

你可能感兴趣的:(uva825 - Walking on the Safe Side(动规))