ACM: 简单 动态规划题 poj 3036

                                                                            Honeycomb Walk

 

Description

ACM: <wbr>简单 <wbr>动态规划题 <wbr>poj <wbr>3036

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 231.

Sample Input

2
2
4

Sample Output

6
90

 

题意: 蜜蜂可以在当前区域走向相邻的6个区域, 问你蜜蜂走n步可以回到原来的区域有多少种走法.

 

解题思路:

          1. 动态规划状态: dp[k][i][j]: 表示在到i,j区域的时候有多少种走法.(1<= i,j <= 16)

          2. 状态转移方程: dp[k][i][j] += dp[k-1][i-dir[t][0]][j-dir[t][1]]; (0<t<6);

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 17

int dir[6][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}, {1,-1}, {-1,1}};

int n;
int dp[MAX][MAX][MAX];

void DP()
{
 memset(dp,0,sizeof(dp));
 dp[0][8][8] = 1;
 for(int k = 1; k <= 14; ++k)
 {
  for(int i = 1; i < MAX; ++i)
  {
   for(int j = 1; j < MAX; ++j)
   {
    for(int t = 0; t < 6; ++t)
    {
     dp[k][i][j] += dp[k-1][i-dir[t][0]][j-dir[t][1]];

    }
   }
  }
 }
}

int main()
{
// freopen("input.txt","r",stdin);
 DP();
 int caseNum;
 scanf("%d",&caseNum);
 while(caseNum--)
 {
  scanf("%d",&n);
  printf("%d\n",dp[n][8][8]);
 }

 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值