HDU--杭电--2323--Honeycomb Walk--DP

Honeycomb Walk

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 111    Accepted Submission(s): 69


Problem Description
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
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
For each test case, output one line containing the number of walks. Under the assumption 1 ≤ n ≤ 14, the answer will be less than 2 31 - 1.
 

Sample Input
  
  
2 2 4
 

Sample Output
  
  
6 90

题意就是像蜂巢那样的一个点与六个点相邻,从一个点开始走,走了n步之后回到远点的路线数

用三位数组来暴力DP,dp[i][j][k]代表的是走第i步走到坐标为(j,k)的点的方法数

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int dp[22][22][22]={0},xx[6][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,-1}};	//因为要回原点,所以最远也只会和原点相距14/2的距离,所以取(7,7)为原点(这个原点是随便去的)
void DP()
{
    int i,j,k,l,a,b;
    dp[0][7][7]=1;	//初始化
    for(i=1;i<=14;i++)	//最多14步
    for(j=0;j<=14;j++)	//遍历可到的全部坐标点
    for(k=0;k<=14;k++)
    for(l=0;l<6;l++)	//遍历相邻的各个方向
    dp[i][j][k]+=dp[i-1][j+xx[l][0]][k+xx[l][1]];
}
int main (void)
{
    int n,m,i,j,k,l;
    DP();
    scanf("%d",&n);
    while(n--&&scanf("%d",&m))
    {
        printf("%d\n",dp[m][7][7]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值