A chess knight can move as indicated in the chess diagram below:

 .           

This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makes N-1 hops.  Each hop must be from one key to another numbered key.

Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressing N digits total.

How many distinct numbers can you dial in this manner?

Since the answer may be large, output the answer modulo 10^9 + 7.

Example 1:

Input: 1
Output: 10

Example 2:

Input: 2
Output: 20

Example 3:

Input: 3
Output: 46

Note:

  • 1 <= N <= 5000

Approach #1: DP. [Java]

class Solution {
public int knightDialer(int N) {
int mod = 1000000007;
int[][][] dp = new int[N+1][5][4]; for (int j = 0; j < 4; ++j)
for (int k = 0; k < 3; ++k)
dp[1][j][k] = 1;
dp[1][3][0] = dp[1][3][2] = 0;
int[][] dirs = {{1, 2}, {1, -2}, {2, 1}, {2, -1},
{-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}}; for (int k = 2; k <= N; ++k) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
if (i == 3 && j != 1) continue;
for (int d = 0; d < 8; ++d) {
int x_ = i + dirs[d][0];
int y_ = j + dirs[d][1];
if (x_ < 0 || y_ < 0 || x_ >= 4 || y_ >= 3) continue;
dp[k][i][j] = (dp[k][i][j] + dp[k-1][x_][y_]) % mod;
}
}
}
} int ans = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
ans = (ans + dp[N][i][j]) % mod;
// System.out.print(dp[N][i][j] + " ");
}
// System.out.println("ans = " + ans);
} return ans;
}
}

Analysis:

We can define dp[k][i][j] as of ways to dial and the last key is (i, j) after k steps

Note: dp[*][3][0], dp[*][3][2] are always zero for all the steps.

Init: dp[0][i][j] = 1

Transition: dp[k][i][j] = sum(dp[k-1][i+dy][j+dx]) 8 ways of move from last step.

ans = sum(dp[k])

Time complexity: O(kmn) or O(k*12*8) = O(k)

Space complexity: O(kmn) -> O(12 * 8) = O(1)

  

Approach #2: DP. [C++]

class Solution {
public:
int knightDialer(int N) {
vector<vector<int>> dp(4, vector<int>(3, 1));
dp[3][0] = dp[3][2] = 0;
int mod = pow(10, 9) + 7;
vector<pair<int, int>> dirs = {{1, 2}, {1, -2}, {2, 1}, {2, -1},
{-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}};
for (int k = 2; k <= N; ++k) {
vector<vector<int>> temp(4, vector<int>(3, 0));
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
if (i == 3 && j != 1) continue;
for (int k = 0; k < 8; ++k) {
int x_ = i + dirs[k].first;
int y_ = j + dirs[k].second;
if (x_ < 0 || y_ < 0 || x_ >= 4 || y_ >= 3) continue;
temp[i][j] = (temp[i][j] + dp[x_][y_]) % mod;
}
}
}
dp.swap(temp);
} int ans = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
ans = (ans + dp[i][j]) % mod;
}
} return ans;
}
};

  

define dp[k][i] as of ways to dial and the last key is i after k steps

init: dp[0][0:10] = 1

translation: dp[k][i] = sum(dp[k-1][j]) that j  can move to i

ans: sum(dp[k])

Time complexity: O(k*10) = O(k)

Space complexity: O(k*10) -> O(10) = O(1).

Reference:

https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-935-knight-dialer/

935. Knight Dialer的更多相关文章

  1. [LeetCode] 935. Knight Dialer 骑士拨号器

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  2. LeetCode 935. Knight Dialer

    原题链接在这里:https://leetcode.com/problems/knight-dialer/ 题目: A chess knight can move as indicated in the ...

  3. 【leetcode】935. Knight Dialer

    题目如下: A chess knight can move as indicated in the chess diagram below:  .            This time, we p ...

  4. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  5. [Swift]LeetCode935. 骑士拨号器 | Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  6. 109th LeetCode Weekly Contest Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  7. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. Android之Dialer之紧急号码

    Android之Dialer之紧急号码 e over any other (e.g. supplementary service related) number analysis. a) 112 an ...

随机推荐

  1. Spring框架的IOC之注解方式的快速入门

    1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:Spring框架的AOP的jar包,spring-aop的jar包 2. 步骤二:创建对应的包结构, ...

  2. .Net多线程 并行编程(三)---并行集合

    为了让共享的数组,集合能够被多线程更新,我们现在(.net4.0之后)可以使用并发集合来实现这个功能. 而System.Collections和System.Collections.Generic命名 ...

  3. CSS3 @keyframes 规则以及animation介绍和各种动画样式说明

    一个好网站:http://www.jqhtml.com/ 如需在 CSS3 中创建动画,您需要学习 @keyframes 规则. @keyframes 规则用于创建动画.在 @keyframes 中规 ...

  4. LFS(Linux From Scratch)学习

    一.简介 LFS──Linux from Scratch,就是一种从网上直接下载源码,从头编译LINUX的安装方式.它不是发行版,只是一个菜谱,告诉你到哪里去买菜(下载源码),怎么把这些生东西( ra ...

  5. Object类中通用方法之:toString()方法

    1.Java所有的对象都是Object类的实例,都可以直接调用该类中定义的方法,这些方法称为通用方法 2.toString()方法即为Object类中定义的通用方法之一 3.平时我们如果在控制台直接打 ...

  6. PAT 1084 外观数列(20)(代码+思路+推荐测试用例)

    1084 外观数列(20 分) 外观数列是指具有以下特点的整数序列: d, d1, d111, d113, d11231, d112213111, ... 它从不等于 1 的数字 d 开始,序列的第 ...

  7. Laravel + Vue 之 OPTIONS 请求的处理

    问题: 在 Vue 对后台的请求中,一般采用 axios 对后台进行 Ajax 交互. 交互发生时,axios 一般会发起两次请求,一次为 Options 试探请求,一次为正式请求. 由此带来的问题是 ...

  8. FTP中各文件目录的说明

    DirectAdmin:FTP中各文件目录的说明     当您使用FTP连上空间后,FTP列表会出现以下文件和目录: domains目录:网站文件存放目录:public_html目录:快捷目录,可以快 ...

  9. Winfrom 嵌入word、excel实现源码

    效果图: winform中嵌入word的方法有多种:调用API,使用webBroser或使用DSOFRAMER控件: API过于繁琐: webbroser读取小文件还行,大文件就太痛苦了: 所以还是选 ...

  10. 解决Error creating bean with name 'huayuanjingguanDaoimp' defined in file [D:\apache-tomcat-7.0.52\webapps\landscapings\WEB-INF\classes\com\itheima\landscaping\dao\imp\huayuanjingguanDaoimp.class]: Invo

    问题描述: 10:23:13,585 ERROR ContextLoader:307 - Context initialization failedorg.springframework.beans. ...