首页 > 其他 > 详细

[动态规划]leetcode 935 Knight Dialer

时间:2019-08-03 17:23:10      阅读:79      评论:0      收藏:0      [点我收藏+]

problem:https://leetcode.com/problems/knight-dialer

        可能是一个比较蠢的dp做法,因为我手动把跳转的方法写成了表:

class Solution {
public:
    vector<vector<int>> next;
int knightDialer(int N) {
        next.resize(10);
        next[0] = { 4, 6 };
        next[1] = { 6, 8 };
        next[2] = { 7, 9 };
        next[3] = { 4, 8 };
        next[4] = { 0, 3, 9 };
        next[5] = { };
        next[6] = { 0, 1, 7 };
        next[7] = { 2, 6 };
        next[8] = { 1, 3 };
        next[9] = { 2, 4 };
        vector<vector<int>> dp(N + 1, vector<int>(10, 0));
        for(int i = 0;i <= 9; i++)
        {
            dp[1][i] = 1;
        }
        for(int n = 2; n <= N; n++)
        {
            for(int i = 0;i <= 9; i++)
            {
                for(auto& idx : next[i])
                {
                    dp[n][i] = (dp[n][i] + dp[n - 1][idx]) % 1000000007;
                }
            }
        }
        
        int res = 0;
        for(int i = 0;i <= 9;i++)
        {
            res = (res + dp[N][i])  % 1000000007;
        }
        
        return res;
    }
};

 

[动态规划]leetcode 935 Knight Dialer

原文:https://www.cnblogs.com/fish1996/p/11295449.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!