为什么不能用记忆化搜索?
查看原帖
为什么不能用记忆化搜索?
151712
一架飞机楼主2021/4/5 14:59
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<iostream>
#include<set>
using namespace std;
typedef long long ll;
template <typename T>inline void read(T&x){
	x=0; char temp=getchar(); bool f=false;
	while(!isdigit(temp)){if(temp=='-') f=true; temp=getchar();}
	while(isdigit(temp)){x=(x<<1)+(x<<3)+temp-'0'; temp=getchar();}
	if(f) x=-x;
}
template <typename T>void print(T x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) print(x/10);
	putchar(x%10+'0');
}
const int M=1005,MOD=1e9+7;
int dp[M][M][2],n,k;
int dfs(int x,int y,int z){//cout<<x<<' '<<y<<' '<<z<<endl;
	if(x>n||x<1||y<=1)return 1;
	if(dp[x][y][z])return dp[x][y][z];
	if(z)dp[x][y][z]=((x+1<=n?dfs(x+1,y,z):1)+(x-1>=1&&y>1?dfs(x-1,y-1,!z):1))%MOD;
	else dp[x][y][z]=((x-1>=1?dfs(x-1,y,z):1)+(x+1<=n&&y>1?dfs(x+1,y-1,!z):1))%MOD;
}
int main(){
	int T;read(T);
	while(T--){
		memset(dp,0,sizeof(dp));
		read(n),read(k);
		printf("%d\n",dfs(1,k,1));
	}
	return 0;
}

第二个样例就炸了

2021/4/5 14:59
加载中...