zoukankan      html  css  js  c++  java
  • uva825Walking on the safe side

    题意:公园在(1,1)点,火车站在(n,m)点,你需要从公园走到火车站,前进时只能距离车站越来越近不能往回走~路上有些地方有障碍(unsafe)不能走,问总的路线有多少。

    简单的dp,不过dp我是写成函数而不是简答的数组,用a[i][j]=0表示可以走,a[i][j]=1表示不能走。递归调用时注意边界,不要越界。

    代码:

    View Code
     1 #include <stdio.h>
     2 #include <string>
     3 #include <string.h>
     4 #include <sstream>
     5 #include <iostream>
     6 using namespace std;
     7 const int MAXN = 200;
     8 int a[MAXN][MAXN];
     9 #define DEBUG
    10 int n, m;
    11 int dp(int i, int j){
    12     if(i==n && j==m) return 1;
    13     if(!a[i][j]){
    14         if(i<n && j<m) return dp(i+1, j) + dp(i, j+1);
    15         else if(i<n) return dp(i+1, j);
    16         else return dp(i, j+1);
    17     }else 
    18         return 0;
    19 }
    20 int main(){
    21 #ifndef DEBUG
    22     freopen("in.txt", "r", stdin);
    23 #endif
    24     int cas, line=0;
    25     scanf("%d", &cas);
    26     while(cas--){
    27         memset(a, 0, sizeof(a));
    28         scanf("%d%d", &n, &m);
    29         int i, j;
    30         string data;
    31         getchar();
    32         for(i=1; i<=n; i++){
    33             getline(cin, data);
    34             istringstream sin(data);
    35             int x;
    36             for(j=0; sin>>x; j++)
    37                 if(j) a[i][x] = 1;
    38         }
    39         if(line++) printf("\n");
    40         printf("%d\n", dp(1, 1));
    41     }
    42     return 0;
    43 }

    这题目就是输入有点小麻烦,个人不是很想用c的char什么的,用了stringstream解决的。

    Greatness is never a given, it must be earned.
  • 相关阅读:
    vue2.0是不支持通过下标来更改数组的,无法做到响应式
    C# 深拷贝 Bitmap对象示例
    vscode终端中文乱码
    TkbmMemTable使用总结
    openssl 证书概念介绍
    openssl源码介绍
    python变量赋值特性
    openssl安装
    github开源协议选择
    NLP 多分类神经网络
  • 原文地址:https://www.cnblogs.com/zjutzz/p/2910759.html
Copyright © 2011-2022 走看看