zoukankan      html  css  js  c++  java
  • UVa 825【简单dp,递推】

    UVa 825

    题意:给定一个网格图(街道图),其中有一些交叉路口点不能走。问从西北角走到东南角最短走法有多少种。(好像没看到给数据范围。、。)

    简单的递推吧,当然也就是最简单的动归了。显然最短路长度就是row+col。求种数就从开始往后推。

    由于第一行第一列也有可能是障碍点,所以初始化时要注意这一点,或者干脆就只初始化f[0][1]=1。i、j都从1开始递推到更方便。还有题目输入输出比较坑。输入我用的是sstream和stream,方便很多,要不还要按照字符串输入再手动转化成数字。输出让每组隔一行,但最后一组没有,用while(T)控制。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<string>
     6 #include<sstream>
     7 using namespace std;
     8 const int maxn = 4006;
     9 int g[maxn][maxn], f[maxn][maxn];
    10 int row, col, res;
    11 
    12 int main()
    13 {
    14     int T;
    15     scanf("%d", &T);
    16     while (T--)
    17     {
    18         memset(g, 0, sizeof(g));
    19         scanf("%d%d", &row, &col);
    20         char cc = getchar();
    21         string line;
    22         int tmp;
    23         for (int i = 1; i <= row; i++)
    24         {
    25             getline(cin, line);
    26             stringstream ss(line);
    27             int cn = 0;
    28             while (ss >> tmp) {
    29                 cn++;
    30                 if (cn>1)
    31                     g[i][tmp] = 1;
    32             }
    33         }
    34         memset(f, 0, sizeof(f));
    35         f[0][1] = 1;
    36         for (int i = 1; i <= row; i++) {
    37             for (int j = 1; j <= col; j++) {
    38                 if (g[i][j]) continue;
    39                 f[i][j] = f[i - 1][j] + f[i][j - 1];
    40             }
    41         }
    42         printf("%d
    ", f[row][col]);
    43         if (T) printf("
    ");
    44     }
    45     return 0;
    46 }

    转载于:https://www.cnblogs.com/zxhyxiao/p/7375298.html

  • 相关阅读:
    Two Sum II
    Subarray Sum
    Intersection of Two Arrays
    Reorder List
    Convert Sorted List to Binary Search Tree
    Remove Duplicates from Sorted List II
    Partition List
    Linked List Cycle II
    Sort List
    struts2结果跳转和参数获取
  • 原文地址:https://www.cnblogs.com/twodog/p/12139612.html
Copyright © 2011-2022 走看看