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

2014-11-24 02:51:46 · 作者: · 浏览: 1

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< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3VwPiAtIDEuPGJyPgoKIAo8YnI+ClNhbXBsZSBJbnB1dAoKPHByZSBjbGFzcz0="brush:java;">2 2 4
Sample Output
6
90

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

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

#include 
    
      #include 
     
       #include 
      
        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; }