109th LeetCode Weekly Contest Knight Dialer
2024-08-29 19:08:46
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
说就是一个马在键盘上跳,能得到多少种的数字,比如1就是不跳。。在原地就是0到9,10个数
那么可以看看,0这个位置只有4和6可以跳过来,1是6,8可以跳过来,5是不可能到的,所以就有下面的代码(参考别人的,自己写的不好看)
class Solution {
public:
int mod = ;
int dp[],dp2[];
int knightDialer(int N) {
for(int i=;i<=;i++){
dp[i]=;
}
for(int i=;i<N;i++){
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = ((dp[]+dp[])%mod+dp[])%mod;
dp2[] = ;
dp2[] = ((dp[]+dp[])%mod+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
dp2[] = (dp[]+dp[])%mod;
for(int j=;j<=;j++){
dp[j] = dp2[j];
}
}
int result = ;
for(int i=;i<=;i++){
cout<<dp[i]<< " ";
result += dp[i] %mod;
result %= mod;
}
cout<<endl;
return result;
}
};
最新文章
- CSS之A标签
- 如何利用tomcat和cas实现单点登录(1):配置tomcat的ssl和部署cas
- c++中两个类互相引用的问题
- C# 给软件加注册码功能
- css笔记12:块元素和行内元素
- CentOS下Redis 2.2.14安装配置详解(转载)
- 参数化时按行读取txt文件,如何去掉换行符";\n";
- centos下一个bash: XXX: command not found解决方案
- 关于第一次STM32连接电脑下载程序
- Session、Cookie 学习笔记
- window.open()参数详解及对浏览器的兼容性
- java实现 批量转换文件编码格式
- 【C语音基础】printf()用法
- Linux下weblogic10.3.6(jar)版本安装详解
- ZOJ 3690 Choosing number(矩阵)
- How to use external classes and PHP files in Laravel Controller?
- 微擎开启redis memcache
- Spring Data JPA @Column 注解无效 打出的语句有下划线
- C++11 std::shared_ptr总结与使用
- Java如何删除数组中的元素?