package com.ant.learning.part1; import java.util.Arrays; import java.util.HashMap; import java.util.List; /** * * 骑士拨号 leetcode.935 ::DP * Created by wolf 2018/11/9 */ public class knightDialer { public static void main(String[] args) { System.out.println( knightDialer(2)); } public static long knightDialer(Integer n){ long mod = (long) (Math.pow(10,9) + 7); int[][] willTobe = {{4,6},{6,8},{7,9},{4,8},{3,9,0},{},{1,7,0},{2,6},{1,3},{2,4}}; int[] res = {1,1,1,1,1,1,1,1,1,1}; if(n==1) return 10; while ( n>1 ){ int[] new_ = {0,0,0,0,0,0,0,0,0,0}; for(int count=0;count<10;count++){ for(int choice:willTobe[count]){ new_[choice]+=res[count]; } } res=new_; n--; } return sum(res)%mod; } private static long sum(int[] nums){ long res=0; for (long num:nums){ res+=num; } return res; } }