C.Planar Reflections(思维+记忆化搜索)

58 篇文章 0 订阅
这篇博客探讨了一个竞赛编程问题,其中涉及血量和向左或向右的反射。作者通过动态规划(DFS + 记忆化搜索)解决超时问题,详细解释了如何计算每个点的贡献,并给出了C++代码实现。博客内容涵盖了算法优化和竞赛编程策略。
摘要由CSDN通过智能技术生成

https://codeforces.com/contest/1498/problem/C


思路:把一个点产生的贡献,看成一个为向左反射血量-1的贡献+一个继续向右血量不变的贡献。直接dfs超时,上记忆化

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
const LL mod=1e9+7;
LL dp[maxn][maxn][3]={0};
LL n,k;
LL dfs(LL x,LL y,LL to){///x为血量,y为层数,to为方向
    ///cout<<"x="<<x<<" "<<"y="<<y<<" "<<"to="<<to<<"\n";
    if(dp[x][y][to]) return dp[x][y][to];
    if(x==1) return dp[x][y][to]=1;
    if(y==n+1&&to==1) return dp[x][y][to]=1;
    if(y==0&&to==0) return dp[x][y][to]=1;
    if(to==1){
        return dp[x][y][to]=(dfs(x-1,y-1,0)%mod+dfs(x,y+1,to)%mod)%mod;
    }
    else return dp[x][y][to]=(dfs(x-1,y+1,1)%mod+dfs(x,y-1,to)%mod)%mod;
}
int main(void)
{
  	cin.tie(0);std::ios::sync_with_stdio(false);
    LL T;cin>>T;
    while(T--){
        cin>>n>>k;
        memset(dp,0,sizeof(dp));
        //这样清空咋wa了
//        for(LL i=0;i<=n+10;i++){
//            for(LL j=0;j<=k+10;j++){
//                for(LL f=0;f<=1;f++){
//                    dp[i][j][f]=0;
//                }
//            }
//        }
        cout<<dfs(k,1,1)<<"\n";
    }
return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值