牛客算法周周练3 B--「木」迷雾森林(dp记忆化搜索+快速读入模板)

地址:https://ac.nowcoder.com/acm/contest/5338/B

     解析:dfs超时了....其实对于每个可到达点来讲,它的来源是左和下,所以有:dp[i][j]=(dp[i+1][j]+dp[i][j-1])

        题目给出了快速读入模板:

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;
}

        完整代码:

#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<map>
#include<cmath>
typedef long long ll;
using namespace std;
int n ,  m;
const int maxn=3e3+10;
int mp[maxn][maxn];
int dp[maxn][maxn];
int sum = 0 ;
const int mod=2333;
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 main()
{
//    memset(vis,0,sizeof(vis));
    read(n);
    read(m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            read(mp[i][j]);
    }
    dp[n][1]=1;
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j]==1)
                continue;
            if(i<n)
                dp[i][j]=(dp[i+1][j]+dp[i][j])%mod;  //i==n时来源只有左没有下
            if(j>1)      
                dp[i][j]=(dp[i][j]+dp[i][j-1])%mod;  //j==1来源只有下没有左
        }
    }
    cout<<dp[1][m]<<endl;
}
原文地址:https://www.cnblogs.com/liyexin/p/12818648.html