蓝桥杯 振兴中华(简单dp)

题目

从我做起振
我做起振兴
做起振兴中
起振兴中华

四连通,从左上角出发到右下角

求"从我做起振兴中华"的方案数

思路来源

https://blog.csdn.net/qq_33302004/article/details/79632132

题解

网上一堆搜索代码长得GG

这明明是个填空题,自己也填出了35

但感觉这种题总是不稳,还是总结一下经验

和立刻出行杯一个按对角线遍历的题很像

当然也可以按行按列扫

代码1

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll dp[6][6];
bool check(int x,int y)
{
	return x>=1&&x<=4&&y>=1&&y<=5;
}
int main()
{
	dp[1][1]=1;
	for(int len=3;len<=9;++len)
	{
		for(int r=1;r<=min(4,len-1);++r)
		{
			int c=len-r;
			if(check(r-1,c))dp[r][c]+=dp[r-1][c];
			if(check(r,c-1))dp[r][c]+=dp[r][c-1];
		}
	}
	printf("%lld\n",dp[4][5]);
	return 0;
} 

代码2

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int dp[5];
int main()
{
	//dp[i][j]=dp[i][j-1]+dp[i-1][j]
	//化简得,i增序,j增序 dp[j]+=dp[j-1]
	//行增序dp即可 
	for(int j=0;j<=4;++j)dp[j]=1;
	for(int i=1;i<=3;++i)
	{
		for(int j=1;j<=4;++j)
		dp[j]+=dp[j-1];
	} 
	printf("%d\n",dp[4]);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Code92007/article/details/88697363