oj2568: 统计方案

题目要求
在一无限大的二维平面中,我们做如下假设:

1、每次只能移动一格;

2、不能向后走(假设你的目的地是“向上”,那么你可以向左走,可以向右走,也可以向上走,但是不可以向下走);

3、走过的格子立即塌陷无法再走第二次。

求走n步不同的方案数(2种走法只要有一步不一样,即被认为是不同的方案)。

Input

首先给出一个正整数C,表示有C组测试数据。

接下来的C行,每行包含一个整数n(n<=20),表示要走n步。

Output

请编程输出走n步的不同方案总数;

每组的输出占一行。

扫描二维码关注公众号,回复: 10514889 查看本文章

Sample Input
Raw
2
1
2
Sample Output
Raw
3
7
根据题目要求,a[1]=3,a[2]=7,以后每次走一步进行分析。
向上走有左右上三个方向,向左走有向上向左两个方向,向右走有向上向右两个方向
因此可以推出a[3]=17,a[4]=41。可以判断条件为a[i] = a[i - 2] + 2 * a[i - 1];

完整代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
	int m,n;
	cin>>n;
	int a[30];
	while(n--)
	{
		cin>>m;
	a[1] = 3;
    a[2] = 7;
    for(int i = 3; i <= m; i++)
        a[i] = a[i - 2] + 2 * a[i - 1];
	cout<<a[m]<<endl;
	}
	return 0;
}
发布了38 篇原创文章 · 获赞 27 · 访问量 3181

猜你喜欢

转载自blog.csdn.net/qq_45891413/article/details/104979266