B-「木」迷雾森林(牛客算法周周练3)(简单动规+记忆化搜索)

示例1

输入

3 3
0 1 0
0 0 0
0 0 0

输出

3

备注:

对于30%的数据,n,m≤100
对于100%的数据,n,m≤3,000
数据规模较大,请使用较快的输入方式,以下为快速读入模板
template<class T>inline void read(T &res)//快速读入模板
{
    char c;
    T flag=1;
    while((c=getchar())<'0'||c>'9')
        if(c=='-')
            flag=-1;
    res=c-'0';
    while((c=getchar())>='0'&&c<='9')
        res=res*10+c-'0';
    res*=flag;
}



scanf("%d",&x)  ->  read(x)
cin>>x -> read(x)

(调用方式:read(要读入的数))

题意:中文题,不过多叙述题意。

思路:这道题的话,题里让从左下角->右上角,是非常典型的DP,然后我们推一下递推方程:

dp[i][j]=dp[i+1][j]+dp[i][j-1],表示当前点是由左侧\left (i+1,j\right )和下侧\left ( i,j-1 \right )得来的,有了递推方程,再做这道题就很轻松了。

AC代码:

#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
typedef long long ll;
const int maxx=110;
const int mod=2333;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
using namespace std;
template<class T>inline void read(T &res)//快速读入模板
{
    char c;
    T flag=1;
    while((c=getchar())<'0'||c>'9')
        if(c=='-')
            flag=-1;
    res=c-'0';
    while((c=getchar())>='0'&&c<='9')
        res=res*10+c-'0';
    res*=flag;
}
int dp[maxx][maxx];
int a[maxx][maxx];
int main()
{
    int m,n;
    read(m),read(n);
    for(int i=1; i<=m; i++)
    {
        for(int j=1; j<=n; j++)
        {
            read(a[i][j]);
        }
    }
    memset(dp,0,sizeof(dp));
    dp[m][1]=1;
    for(int i=m; i>=1; i--)
    {
        for(int j=1; j<=n; j++)
        {
            if(a[i][j]==1)
                continue;
            if(i!=n || j!=1)
                dp[i][j]=(dp[i+1][j]+dp[i][j-1])%mod;
        }
    }
    printf("%d\n",dp[1][n]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值