HDU 2323 Honeycomb Walk

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 231- 1.
Sample Input
2
2
4
Sample Output
6
90
分析:

可以把每个六边形抽象成一个点,然后再进一步抽象成一个坐标平面。这样的话,一个状态就可以用到达这一状态所用的步数k,这一状态所在的x坐标,y坐标来表示。状态的目标函数自然就是到达这一状态可能的路径数。接下来,就是状态转移方程了。

Way[k][i][j]=way[k-1][i][j+1]+way[k-1][i][j-1]+way[k-1][i-1][j]

+way[k-1][i-1][j-1]+way[k-1][i+1][j] +way[k-1][i+1][j+1]

循环时,首先是k,然后i,j

code:
View Code
#include<stdio.h>
int way[15][17][17];
int list[15];
int main()
{
int k,n,i,j,t;
way[0][8][8]=1;
for(k=1;k<=14;k++)
{
for(i=0;i<17;i++)
for(j=1;j<17;j++)
way[k][i][j]=way[k-1][i-1][j]+way[k-1][i][j+1]
+way[k-1][i+1][j]+way[k-1][i+1][j+1]
+way[k-1][i][j-1]+way[k-1][i-1][j-1];
list[k]=way[k][8][8];
}
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",list[n]);
}
return 0;
}

 
posted @ 2012-03-14 17:52  'wind  阅读(800)  评论(0编辑  收藏  举报