2489. Honeycomb Walk

一次性AC,此问题为动态规划的问题,题意为一直蜜蜂在一个六角形的格子里面,它向其临近的六边形走都有可能,给定一个N,让蜜蜂在走N步后还是回到起始的格子里面,问有多少种解法。用动态规划的问题解决。将六角形抽象到坐标轴中,用三维数组dp[k][i][j]来表示,其中k为达到(i,j)这个点所用的步数,由于k<=14,故蜜蜂不能走出距离其大于7个格子,因为最终要回到原点,令(8,8)为起始地点,dp[k][8][8]代表走k步后回到原点的走法数量,即为我们要求得的数据。用关于k的循环一层层求。

A bee larva living in a hexagonal cell of a large honeycomb decides to creep for a walk. In each "step" the larva may move into any of the six adjacent cells and after n steps, it is to end up in its original cell.

Your program has to compute, for a given n, the number of different such larva walks.

Input specifications

The first line contains an integer giving the number of test cases to follow. Each case consists of one line containing an integer n, where 1 ≤ n ≤ 14.

Output specifications

For each test case, output one line containing the number of walks. Under the assumption 1 ≤ n ≤ 14, the answer will be less than 231.

Sample input

2
2
4

Output for sample input

6
90

#include<iostream>
using namespace std;
int dp[15][17][17],i,a[15],j,k,n,N;//数组dp[k][i][j]指的是到达坐标为(i,j)所用的步数为k步
int main(){	
	//数组定义在外面初始值为0;
	dp[0][8][8]=1;//由于最大的步数为14,蜜蜂要回到原地,所以最远的距离不会超过7,令(8,8)为原点
    for(k=1;k<=14;k++){
		for(i=1;i<=16;i++)
			for(j=1;j<=16;j++)
				dp[k][i][j]=dp[k-1][i][j-1]+dp[k-1][i][j+1]+dp[k-1][i-1][j]+dp[k-1][i+1][j]+dp[k-1][i+1][j+1]+dp[k-1][i-1][j-1];
		a[k]=dp[k][8][8];
	}
	cin>>N;
	while(N--){
		cin>>n;
		cout<<a[n]<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值