• HDU 1978 How many ways


    How many ways

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 1978
    64-bit integer IO format: %I64d      Java class name: Main
     
    这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m)。游戏的规则描述如下:
    1.机器人一开始在棋盘的起始点并有起始点所标有的能量。
    2.机器人只能向右或者向下走,并且每走一步消耗一单位能量。
    3.机器人不能在原地停留。
    4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量。

    如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点是(2,4)

    点,当他到达(2,4)点时将拥有1单位的能量,并开始下一次路径选择,直到到达(6,6)点。
    我们的问题是机器人有多少种方式从起点走到终点。这可能是一个很大的数,输出的结果对10000取模。
     

    Input

    第一行输入一个整数T,表示数据的组数。
    对于每一组数据第一行输入两个整数n,m(1 <= n,m <= 100)。表示棋盘的大小。接下来输入n行,每行m个整数e(0 <= e < 20)。
     

    Output

    对于每一组数据输出方式总数对10000取模的结果.
     

    Sample Input

    1
    6 6
    4 5 6 6 4 3
    2 2 3 1 7 2
    1 1 4 6 2 7
    5 8 4 3 9 5
    7 6 6 2 1 5
    3 1 1 3 7 2

    Sample Output

    3948

    Source

     
    解题:记忆化搜索
     1 /*
     2 @author: Lev
     3 @date:
     4 */
     5 #include <iostream>
     6 #include <cstdio>
     7 #include <cmath>
     8 #include <cstring>
     9 #include <string>
    10 #include <cstdlib>
    11 #include <algorithm>
    12 #include <map>
    13 #include <set>
    14 #include <queue>
    15 #include <climits>
    16 #include <deque>
    17 #include <sstream>
    18 #include <fstream>
    19 #include <bitset>
    20 #include <iomanip>
    21 #define LL long long
    22 #define INF 0x3f3f3f3f
    23 
    24 using namespace std;
    25 int dp[102][102],mp[102][102],n,m;
    26 bool isOk(int x,int y) {
    27     if(x < 0 || x >= n || y < 0 || y >= m) return false;
    28     return true;
    29 }
    30 int dfs(int x,int y) {
    31     if(dp[x][y] >= 0) return dp[x][y];
    32     dp[x][y] = 0;
    33     for(int i = 0; i <= mp[x][y]; ++i)
    34         for(int j = 0; j <= mp[x][y]-i; ++j)
    35             if(isOk(x+i,y+j)) dp[x][y] = (dp[x][y] + dfs(x+i,y+j))%10000;
    36     return dp[x][y];
    37 }
    38 int main() {
    39     int kase;
    40     scanf("%d",&kase);
    41     while(kase--) {
    42         scanf("%d %d",&n,&m);
    43         for(int i = 0; i < n; ++i)
    44             for(int j = 0; j < m; ++j)
    45                 scanf("%d",mp[i]+j);
    46         memset(dp,-1,sizeof(dp));
    47         dp[n-1][m-1] = 1;
    48         printf("%d
    ",dfs(0,0));
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    linux cmake安装方法
    Linux下安装numpy
    linux下安装opencv3.0
    linux升级gcc
    php session获取不到的解决方法
    python Tesseract安装方法
    SQLite-编译指示
    第十章:定积分
    7. 错误、调试和测试
    钱纳里的工业化阶段理论
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4319332.html
Copyright © 2020-2023  润新知