514. 自由之路
电子游戏“辐射4”中,任务“通向自由”要求玩家到达名为“Freedom Trail Ring”的金属表盘,并使用表盘拼写特定关键词才能开门。
给定一个字符串 ring,表示刻在外环上的编码;给定另一个字符串 key,表示需要拼写的关键词。您需要算出能够拼写关键词中所有字符的最少步数。
最初,ring 的第一个字符与12:00方向对齐。您需要顺时针或逆时针旋转 ring 以使 key 的一个字符在 12:00 方向对齐,然后按下中心按钮,以此逐个拼写完 key 中的所有字符。
旋转 ring 拼出 key 字符 key[i] 的阶段中:
您可以将 ring 顺时针或逆时针旋转一个位置,计为1步。旋转的最终目的是将字符串 ring 的一个字符与 12:00 方向对齐,并且这个字符必须等于字符 key[i] 。
如果字符 key[i] 已经对齐到12:00方向,您需要按下中心按钮进行拼写,这也将算作 1 步。按完之后,您可以开始拼写 key 的下一个字符(下一阶段), 直至完成所有拼写。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/freedom-trail
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
搜索
import java.util.*;
class Solution {
    private static int getDistance(int ringSize, int from, int to) {
        return Math.min(Math.abs(from - to), Math.min(from, to) + ringSize - Math.max(from, to));
    }
    /**
     * 简约递归
     * 可改为记忆化搜索
     */
    private static int solve(Map<Character, List<Integer>> keyMap, int ringSize, int currentPos, int index, char[] aim) {
        if (index == aim.length) {
            return 0;
        }
        /**
         * 枚举所有可能
         */
        List<Integer> list = keyMap.get(aim[index]);
        int ret = Integer.MAX_VALUE;
        for (int pos : list) {
            ret = Math.min(ret, 1 + getDistance(ringSize, currentPos, pos) + solve(keyMap, ringSize, pos, index + 1, aim));
        }
        return ret;
    }
    /**
     * 动态规划
     */
    private static final int dp(Map<Character, List<Integer>> keyMap, int ringSize, char[] aim) {
        int m = aim.length;
        int[][] dp = new int[m + 1][ringSize];
        for (int index = m - 1; index >= 0; --index) {
            List<Integer> list = keyMap.get(aim[index]);
            for (int currentPos = 0; currentPos < ringSize; currentPos++) {
                dp[index][currentPos] = Integer.MAX_VALUE;
                for (int pos : list) {
                    dp[index][currentPos] = Math.min(dp[index][currentPos], 1 + getDistance(ringSize, currentPos, pos) + dp[index + 1][pos]);
                }
            }
        }
        return dp[0][0];
    }
    public static int findRotateSteps(String ring, String key) {
        Map<Character, List<Integer>> keyMap = new HashMap<>();
        for (int i = 0; i < ring.length(); ++i) {
            List<Integer> list = keyMap.getOrDefault(ring.charAt(i), new ArrayList<>());
            list.add(i);
            keyMap.put(ring.charAt(i), list);
        }
        return dp(keyMap, ring.length(), key.toCharArray());
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            System.out.println(findRotateSteps(in.next(), in.next()));
        }
    }
}
动态规划
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
    public int findRotateSteps(String ring, String key) {
        List<Integer>[] poses = new List[26];
        for (int i = 0; i < 26; i++) {
            poses[i] = new ArrayList<>();
        }
        for (int i = 0; i < ring.length(); i++) {
            poses[ring.charAt(i) - 'a'].add(i);
        }
        int n = ring.length();
        int m = key.length();
        int[] dp = new int[n];
        int INF = 30000;
        Arrays.fill(dp, INF);
        for (int pos : poses[key.charAt(0) - 'a']) {
            dp[pos] = Math.min(pos, n - pos) + 1;
        }
        for (int i = 1; i < m; i++) {
            int[] helper = new int[n];
            Arrays.fill(helper, INF);
            for (int j : poses[key.charAt(i) - 'a']) {
                for (int pos : poses[key.charAt(i - 1) - 'a']) {
                    helper[j] = Math.min(helper[j], dp[pos] + Math.min(Math.abs(pos - j), n - Math.abs(pos - j)) + 1);
                }
            }
            System.arraycopy(helper, 0, dp, 0, dp.length);
        }
        return Arrays.stream(dp).min().getAsInt();
    }
}
    心之所向,素履以往 生如逆旅,一苇以航

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号