LeetCode684,获取所有钥匙的最短路径,每日一题20221110

题目

记忆化搜索,状态DP。
给你一个图,图上有些点不能经过,有些点是钥匙,有些点是锁,到达锁之前必须要持有对应的钥匙,问至少走多少步可以拿到所有的钥匙。
详细见LeetCode684,获取所有钥匙的最短路径。

题解

思路

状态DP,对于一个特定的状态,我持有特定的一些钥匙,我只会最多经历任意一个点1次,那么我们可以用宽度优先搜索来求解,状态的话就用二进制表示钥匙持有的状态。
设共有cnt个钥匙。

  1. 起点dp[s_x][s_y][0]
  2. 终点dp[i][j][(1 << cnt) - 1]
  3. 到达锁点,必须要对应的钥匙
  4. 到达#忽略,
  5. 判断是否已到达过

时间复杂度

O(n*m*2^cnt)

空间复杂度

O(n*m*2^cnt)

代码

class Solution {
public:
    int shortestPathAllKeys(vector<string>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        int sx, sy;
        int cnts = 0;
        unordered_map<char, int> mp; // map key to an integer
        // search for starting point
        // record keys
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(grid[i][j] <= 'z' && grid[i][j] >= 'a') {
                    mp[grid[i][j]] = cnts;
                    cnts++;
                }
                if(grid[i][j] == '@') {
                    sx = i;
                    sy = j;
                }
            }
        }
        // cout << sx <<" " << sy <<endl; 
        vector<vector<vector<int>>> dp(n,vector<vector<int>>(m, vector<int>(1 << cnts, -1)));
        queue<tuple<int, int, int>> q;
        q.emplace(sx, sy, 0);
        int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        dp[sx][sy][0] = 0;
        while(q.size()) {
            auto [x, y, mask] = q.front();
            q.pop();
            for(int i = 0; i < 4; i++) {
                int tmpx = x + dir[i][0];
                int tmpy = y + dir[i][1];
                int tmpmask = mask;
                if(tmpx >= n || tmpx < 0 
                || tmpy >= m || tmpy < 0) continue;
                char c = grid[tmpx][tmpy];
                // at key point, need to update mask
                if(c <= 'z' && c >= 'a') {
                    // cout << c << " " << mp[c] << endl;
                    tmpmask = tmpmask | (1 << mp[c]);
                } else if(c <= 'Z' && c >= 'A') { // at lock point, check the associated key
                    if(!(tmpmask & (1 << mp[c - ('A' - 'a')]))) continue;
                } else if(c == '#') { // at blocked point
                    continue;
                }
                if(dp[tmpx][tmpy][tmpmask] != -1) continue ; // already visited before
                dp[tmpx][tmpy][tmpmask] = dp[x][y][mask] + 1; 
                q.emplace(tmpx, tmpy, tmpmask);
                // cout << tmpx << " " << tmpy << " " << bitset<sizeof(tmpmask) * 8>(tmpmask) << endl;
                // at end point
                if(mask == (1 << cnts) - 1) return dp[tmpx][tmpy][tmpmask] - 1;
            }
        }
        return -1;

    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值