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框架的AOP技术(注解方式)

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

  2. linux磁盘管理(RHEL)

    IDE硬盘名称格式为/dev/hdXY,其中X为a-z的小写字母,Y为数字1-4(一块硬盘最多能分4个主分区).如hda1,表示第一块硬盘的第一个分区.hdb3表示第二块硬盘的第三个分区.还有如Pri ...

  3. [原创汉化] 价值990美元的顶级专业数据恢复软件O&O DiskRecovery 11(技术员版)汉化绿色版

    百度没搜索到11有汉化版的,有空就把它汉化了,大部分借鉴的是以前汉化版的词条.另外,顺便做了个二合一的单文件版给有需要的朋友. 运行环境: 可用于 Windows 2000/XP/2003/Vista ...

  4. 2018.10.20 bzoj1079: [SCOI2008]着色方案(多维dp)

    传送门 dp妙题. f[a][b][c][d][e][last]f[a][b][c][d][e][last]f[a][b][c][d][e][last]表示还剩下aaa个可以用一次的,还剩下bbb个可 ...

  5. IntelliJ IDEA 2017版 spring-boot修改端口号配置把端口号改为8081

    1.修改端口号主要是通过配置文件修改.如图: 完整版配置 ######################################################## ###server 配置信息 ...

  6. verilog基础--altera培训

    参数化 Localparam :与prameter一样,但不能被重写. Verilog-2001 格式, module mult_acc #(parameter size = 8 ) (...); 数 ...

  7. Sublime必用快捷键[私人]

    最近一年前端开发都是用sublime这款编辑器, 相对于webStorm强大而启动慢.editplus快启动而功能弱, sublime恰好在两者之间:而且其指令行安装.更新.卸载插件比eclipse之 ...

  8. HDU 1166敌兵布阵 2016-09-14 18:58 89人阅读 评论(0) 收藏

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. Paul and Joyce are going to a movie(More listening of Unit 2)

      Paul: Hurry up, Joyce. We need to leave now if we're going to get to the theater a half hour befor ...

  10. Latex中Matlab代码的环境

    需要用到listings宏包 使用方法: 导言区\usepackage{listings}\lstset{language=Matlab}      %代码语言使用的是matlab\lstset{br ...