动态规划-Minimum Distance to Type a Word Using Two Fingers
2020-01-12 18:28:13
问题描述:


问题求解:
本题还是非常困难的,至少我在看到这个题目的时候是没有想到怎么解决的。我当时联想到的题目是那条grid走两遍的题目,那条题目也很麻烦,使用的是dp。
本题最难的地方在于如何定义状态,其实本题可以看作是个路径规划问题,所以状态就是左指在的位置和右指在位置以及当前的字符位置。
之后递归去遍历解空间即可。
int[][][] dp = new int[27][27][301];
public int minimumDistance(String word) {
return dfs(word, 0, 0, 0);
}
private int dfs(String word, int start, int l, int r) {
if (start >= word.length()) return 0;
if (dp[l][r][start] != 0) return dp[l][r][start];
int idx = word.charAt(start) - 'A' + 1;
dp[l][r][start] = Math.min(dist(l, idx) + dfs(word, start + 1, idx, r), dist(r, idx) + dfs(word, start + 1, l, idx));
return dp[l][r][start];
}
private int dist(int from, int to) {
if (from == 0) return 0;
int x1 = (from - 1) / 6;
int y1 = (from - 1) % 6;
int x2 = (to - 1) / 6;
int y2 = (to - 1) % 6;
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
动态规划-Minimum Distance to Type a Word Using Two Fingers的更多相关文章
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
- [LeetCode&Python] Problem 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- leetcode 783. Minimum Distance Between BST Nodes 以及同样的题目 530. Minimum Absolute Difference in BST
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...
- [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
随机推荐
- iPhone8、Note8、Mate10硬上面部识别:是潮流还是无奈
对于手机厂商来说,时不时抛出几个全新概念当噱头来引起业界和大众的关注,已经成为了必然套路.其中有很多改变智能手机发展进程的技术--双摄像头.指纹识别.快充等,但也有很多纯粹来凑数,看似新潮却始终没 ...
- Node学习(二) --使用http和fs模块实现一个简单的服务器
1.创建一个www目录,存储静态文件1.html.1.jpg. * html文件内容如下: 12345678910111213 <html lang="en">< ...
- MySQL学习之路 一 : MySQL 5.7.19 源码安装
MySQL 5.7.19 源码安装 查看系统: # cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装依赖包 # yum - ...
- Python开发(三):字符编码,文件操作,函数
一:三级菜单 If len(choice) == continue # 判断输入的是否为空,为空就跳出这次循环进行下次循环, exit(“bye”) :退出程序显示,bye 二:编码 最早的编码是as ...
- Java 八种基本类型和基本类型封装类
1.首先,八种基本数据类型分别是:int.short.float.double.long.boolean.byte.char: 它们的封装类分别是:Integer.Short.Float.Doub ...
- Python实战之制作瘟疫传播实验
2020年爆发新型冠状病毒,让大家在见证中国的团结也让大家感受到疫情传播的骇人 在这里先道一声“武汉加油.中国加油” 那么现在我们尝试制作一个模拟疫情爆发的模型,以数字的形式展现疫情爆发点恐怖. (1 ...
- ASP.NET Core 中间件自定义全局异常处理
目录 背景 ASP.NET Core过滤器(Filter) ASP.NET Core 中间件(Middleware) 自定义全局异常处理 .Net Core中使用ExceptionFilter .Ne ...
- Rust入坑指南:智能指针
在了解了Rust中的所有权.所有权借用.生命周期这些概念后,相信各位坑友对Rust已经有了比较深刻的认识了,今天又是一个连环坑,我们一起来把智能指针刨出来,一探究竟. 智能指针是Rust中一种特殊的数 ...
- C++ 标准模板库(STL)-string
总结了一些c++ string库常用的库函数用法 #include <iostream> #include <string>//string类可以自动管理内存 using na ...
- C++ 函数模板/类模板
#include <iostream> #include <vector> using namespace std; template < class T > // ...