id=19217">称号: UVA - 825Walking on the Safe Side(dp)

题目大意:给出一个n * m的矩阵。起点是1 * 1,终点是n * m。这个矩阵上有些点是不能够经过的,要求从起点到终点距离最短,而且不能走那种不能走的点。一共同拥有多少种方式。

解题思路:要求路径最短的话,每一个点要不向右走,要不向下走。dp【i】【j】 = dp【i】【j + 1】 + dp【i + 1】【j】;当这个点不能通过。dp【i】【j】 = 0;这个坑点在例子输入。不一定是规范的输入。可能两个数字之间非常多空格,或者最后一个数字和换行符之间非常多空格。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. const int N = 1005;
  5.  
  6. typedef long long ll;
  7.  
  8. int G[N][N];
  9. ll dp[N][N];
  10. char str[N];
  11.  
  12. void handle () {
  13.  
  14. int x, y;
  15. bool flag = 1;
  16. x = y = 0;
  17. // printf ("%s\n", str);
  18. for (int i = 0; i <= strlen (str); i++) {
  19.  
  20. if (str[i] >= '0' && str[i] <= '9') {
  21.  
  22. if (flag)
  23. x = x * 10 + str[i] - '0';
  24. else
  25. y = y * 10 + str[i] - '0';
  26. } else {
  27.  
  28. if (!flag)
  29. G[x][y] = 1;
  30. // printf ("%d %d\n", x, y);
  31. y = 0;
  32. flag = 0;
  33. }
  34. }
  35. }
  36.  
  37. int main () {
  38.  
  39. int t, n, m;
  40. int x, y;
  41. char ch;
  42. scanf ("%d", &t);
  43. while (t--) {
  44.  
  45. scanf ("%d%d%*c", &n, &m);
  46. memset (G, 0, sizeof (G));
  47. for (int i = 1; i <= n; i++) {
  48.  
  49. gets(str);
  50. handle();
  51. }
  52.  
  53. for (int i = n; i >= 1; i--)
  54. for (int j = m; j >= 1; j--) {
  55.  
  56. dp[i][j] = 0;
  57. if (G[i][j])
  58. continue;
  59.  
  60. if (i == n && j == m) {
  61. dp[i][j] = 1;
  62. continue;
  63. }
  64. if (i != n)
  65. dp[i][j] += dp[i + 1][j];
  66. if (j != m)
  67. dp[i][j] += dp[i][j + 1];
  68. }
  69.  
  70. printf ("%lld\n", dp[1][1]);
  71. if (t)
  72. printf ("\n");
  73. }
  74. return 0;
  75. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVA - 825Walking on the Safe Side(dp)的更多相关文章

  1. UVA 10163 Storage Keepers(两次DP)

    UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...

  2. uva 11584 Partitioning by Palindromes 线性dp

    // uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...

  3. uva 825 - Walking on the Safe Side(dp)

    题目链接:825 - Walking on the Safe Side 题目大意:给出n,m,现在给出n行数据, 每行有k(k为不定值)个数字, 第一个数字代表行数, 后面k - 1个数代表当前行的这 ...

  4. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  5. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

  6. UVA 1401 - Remember the Word(Trie+DP)

    UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...

  7. [uva 11762]Race to 1[概率DP]

    引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...

  8. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

  9. Uva 11077 Find the Permutations [置换群 DP]

    题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...

随机推荐

  1. php模拟post提交文件图片等

    <?php /** * Email net.webjoy@gmail.com * author jackluo * 2014.11.21 * */ //* function curl_post( ...

  2. NetSnmp配置

    http://blog.csdn.net/shanzhizi/article/details/16985989

  3. [CSS Flex] Justify-content

    justify content contol how element inside flex box align, it can be "right / end", or &quo ...

  4. [React] Test friendly approach

    Add functional function such as change state, this should have tests covered. For example, in a comp ...

  5. Erlang 进制转换

    http://www.cnblogs.com/me-sa/archive/2012/03/20/erlang0047.html bnot unary bitwise not integer div i ...

  6. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

  7. Qt 通过绘画设置边框阴影

    首先widget设置 setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground, true) ...

  8. html5-1 网页结构描述

    html5-1 网页结构描述 一.总结 一句话总结:注意head中的title,keywords,description,这对seo优化很有帮助 1.如何给某元素动态使用类似onclick方法? 点o ...

  9. [Django] Get started with Django -- Install python and virtualenv

    Install python3 on MacOS: brew install python3 Come alone with python3, there are also some other to ...

  10. WPF入门(三)->几何图形之不规则图形(PathGeometry) (2)

    原文:WPF入门(三)->几何图形之不规则图形(PathGeometry) (2) 上一节我们介绍了PathGeometry中LineSegment是点与点之间绘制的一条直线,那么我们这一节来看 ...