365天挑战LeetCode1000题——Day 083 优美的排列 优美的排列II 迷宫中离入口最近的出口 最短的桥

本文介绍了三个关于算法实现的题目:优美的排列(包括第一部分和第二部分)、迷宫中离入口最近的出口以及最短的桥。每个题目都提供了C++代码实现,分别涉及位操作、图遍历和深度优先搜索等算法。优美的排列问题关注于找到满足特定条件的排列组合;迷宫问题通过广度优先搜索寻找从入口到出口的最短路径;最短的桥问题则通过深度优先搜索和广度优先搜索结合找到将两个岛屿连接起来的最短路径。

526. 优美的排列

在这里插入图片描述

代码实现(部分看题解)

class Solution {
public:
    int countArrangement(int n) {
        vector<vector<int>> ans(vector(n + 1, vector<int>(1 << n)));
        ans[0][0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int state = 0; state < 1 << n; state++) {
                for (int k = 1; k <= n; k++) {
                    if ((state & (1 << (k - 1))) == 0) {
                        continue;
                    }
                    if (k % i && i % k) {
                        continue;
                    }
                    ans[i][state] += ans[i - 1][state & (~(1 << (k - 1)))];
                }
            }
        }
        return ans[n][(1 << n) - 1];
    }
};

667. 优美的排列II

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    vector<int> constructArray(int n, int k) {
        vector<int> ans;
        for (int i = 1; i < n - k; i++) {
            ans.push_back(i);
        }
        for (int i = n - k, j = n; i <= j; i++, j--) {
            ans.push_back(i);
            if (i != j) {
                ans.push_back(j);
            }
        }
        return ans;
    }
};

1926. 迷宫中离入口最近的出口

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
        int m = maze.size();
        int n = maze[0].size();
        queue<pair<int, int>> myQueue;
        myQueue.push({entrance[0], entrance[1]});
        maze[entrance[0]][entrance[1]] = '+';
        vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        int step = 1;
        while (myQueue.size()) {
            int sz = myQueue.size();
            while (sz--) {
                auto [i, j] = myQueue.front();
                // cout << i << " " << j << endl;
                myQueue.pop();
                for (auto [x, y] : directions) {
                    x += i;
                    y += j;
                    if (x < 0 || y < 0 || x >= m || y >= n) {
                        continue;
                    }
                    if ((x == 0 || x == m - 1 ||y == 0 || y == n - 1) && maze[x][y] == '.') return step;
                    if (maze[x][y] == '.') {
                        maze[x][y] = '+';
                        myQueue.push({x, y});
                    }
                }
            }
            step++;
        }
        return -1;
    }
};

934. 最短的桥

在这里插入图片描述

代码实现(自解)

class Solution {
private:
    set<pair<int, int>> border;
    queue<pair<int, int>> myQueue;
    vector<pair<int, int>> direction;
    int n;
    void dfs(vector<vector<int>>& grid, int i, int j) {
        grid[i][j] = 0;
        myQueue.push({i, j});
        for (auto [x, y] : direction) {
            x += i;
            y += j;
            if (x >= n || y >= n || x < 0 || y < 0) {
                continue;
            }
            if (grid[x][y]) {
                dfs(grid, x, y);
            }
        }
    }
public:
    int shortestBridge(vector<vector<int>>& grid) {
        direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        n = grid.size();
        auto new_grid = grid;
        int m = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (new_grid[i][j] && !m) {
                    dfs(new_grid, i, j);
                    m = 1;
                    continue;
                }
                if (new_grid[i][j]) {
                    border.emplace(make_pair(i, j));
                }
            }
        }
        int ans = 0;
        while (myQueue.size()) {
            int sz = myQueue.size();
            while (sz--) {
                auto [i, j] = myQueue.front();
                myQueue.pop();
                for (auto [x, y] :direction) {
                    x += i, y += j;
                    if (x < 0 || y < 0 || x >= n || y >= n) continue;
                    if (border.count({x, y})) {
                        return ans;
                    }
                    if (!grid[x][y]) {
                        grid[x][y] = 1;
                        myQueue.push({x, y});
                    }
                }
            }
            ans++;
        }
        return -1;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值