zoukankan      html  css  js  c++  java
  • UVa 825 Walking on the Safe Side(简单DP)

    题意:

    有t组测试数据,每组测试数据给一个矩阵n,m。

    接下来给出n行,每行第一个数字为该行的编号(从1开始),然后给出这行不能走的y坐标。

    问从出发点(1,1),到(n,m)有多少种不同的路径。

    思路:

    dfs思想根深蒂固啊,其实是很简单的递推题目,仔细思考再决定要不要深度递归。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    
    const int MAXN = 128;
    bool map[MAXN][MAXN];
    int dp[MAXN][MAXN];
    
    int main()
    {
        int cases;
        scanf("%d", &cases);
        while (cases--)
        {
            int row, col;
            scanf("%d %d", &row, &col);
    
            for (int i = 1; i <= row; ++i)
            {
                for (int j = 1; j <= col; ++j)
                    map[i][j] = true;
    
                char str[MAXN];
                int r, c;
    
                scanf("%d", &r);
                gets(str);
    
                for (int j = 0, c = 0; j <= strlen(str); ++j)
                    if ('0' <= str[j] && str[j] <= '9')
                        c = c * 10 + str[j] - '0';
                    else
                        map[r][c] = false, c = 0;           
            }
            
            memset(dp, 0, sizeof(dp));
    
            dp[1][1] = 1;
            map[1][1] = false;
    
            for (int i = 1; i <= row; ++i)
                for (int j = 1; j <= col; ++j)
                    if (map[i][j])
                        dp[i][j] = dp[i-1][j] + dp[i][j-1];
    
            printf("%d\n", dp[row][col]);
            if (cases)
                printf("\n");
        }
        return 0;
    }

     

    -------------------------------------------------------

    kedebug

    Department of Computer Science and Engineering,

    Shanghai Jiao Tong University

    E-mail: kedebug0@gmail.com

    GitHub: http://github.com/kedebug

    -------------------------------------------------------

  • 相关阅读:
    LeetCode Flatten Binary Tree to Linked List
    LeetCode Longest Common Prefix
    LeetCode Trapping Rain Water
    LeetCode Add Binary
    LeetCode Subsets
    LeetCode Palindrome Number
    LeetCode Count and Say
    LeetCode Valid Parentheses
    LeetCode Length of Last Word
    LeetCode Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/kedebug/p/2772373.html
Copyright © 2011-2022 走看看