出入度|bfs|状压dp

lc1349

状压dp

二进制数表示每行座位的学生就座状态

校验每行及相邻行的就座合法性后

递推计算能坐下的最大学生数

class Solution {

public:

int maxStudents(vector<vector<char>>& seats) {

int m = seats.size();

int n = seats[0].size();

vector<vector<int>> dp(m + 1, vector<int>(1 << n));

for (int row = 1; row <= m; ++row)

for (int i = 0; i < (1 << n); ++i) {

bitset<8> bs(i);

bool ok = true;

for (int j = 0; j < n; ++j) {

if (bs[j] && seats[row-1][j] == '#' || (j < n - 1 && bs[j] && bs[j + 1])) {

ok = false;

break;

}

}

if (!ok) {

dp[row][i] = -1;

continue;

}

for (int last = 0; last < (1 << n); ++last) {

if (dp[row - 1][last] == -1)

continue;

bitset<8> lbs(last);

bool ok = true;

for (int j = 0; j < n; ++j) {

if (lbs[j] && ((j > 0 && bs[j - 1]) || (j < n - 1 && bs[j + 1]))) {

ok = false;

break;

}

}

if (ok)

dp[row][i] = max(dp[row][i], dp[row - 1][last] + (int)bs.count());

}

}

int ans = 0;

for (int i = 0; i < (1 << n); ++i)

ans = max(ans, dp[m][i]);

return ans;

}

};

lcp9

✓小球从编号0的弹簧出发,在第i个弹簧处按动时,可向右弹 jump[i] 距离(弹出去就完成)

或向左弹到任意左侧弹簧(0处不能左弹),求把小球弹出机器的++最少按动弹簧次数++

BFS结合前驱位置优化遍历

向右跳弹簧位置、向左遍历未访问的前驱位置

找到跳出弹簧数组的最少按动次数

class Solution {

public:

int minJump(vector<int>& jump)

{

int n = jump.size();

queue<pair<int, int>> q;

q.emplace(0, 0);

vector<bool> seen(n, false);

++seen[0] = true;++

++//实现每个地方只过一遍的记忆化++

int preidx = 1;

while (!q.empty()) {

auto [idx, d] = q.front();

q.pop();

int next = idx + jump[idx];

if (next > n - 1)

return d + 1;

++if (!seen[next]) {++

seen[next] = true;

++q.emplace(next, d + 1);++

}

++while (preidx < idx) {++

if (!seen[preidx]) {

seen[preidx] = true;

q.emplace(preidx, d + 1);

}

++preidx++;++

}

}

return -1;

}

};

lcp62

/*

交通枢纽: 入度为n-1, 出度为0

*/

class Solution {

public:

int transportationHub(vector<vector<int>>& path) {

int d[1010][2] = {0};

++// d[i][0]: i的入度, d[i][1]: i的出度
unordered_set<int> s; // 存哪些城市
++

for (auto& p : path) {

d[p[0]][1]++, d[p[1]][0]++;

s.insert(p[0]);

s.insert(p[1]);

}

int n = s.size(); // 共n个城市

for (auto city : s)

{ // 交通枢纽: 入度为n-1, 出度为0的城市

if (d[city][0] == n - 1 && d[city][1] == 0) return city;

}

return -1;

}

};

相关推荐
踩坑记录16 小时前
leetcode hot100 easy 101. 对称二叉树 递归 层序遍历 bfs
算法·leetcode·宽度优先
2501_9403152617 小时前
leetcode182动态口令(将字符的前几个元素放在字符串后面)
算法
老鼠只爱大米18 小时前
LeetCode经典算法面试题 #98:验证二叉搜索树(递归法、迭代法等五种实现方案详解)
算法·leetcode·二叉树·递归·二叉搜索树·迭代
疯狂的喵1 天前
C++编译期多态实现
开发语言·c++·算法
scx201310041 天前
20260129LCA总结
算法·深度优先·图论
2301_765703141 天前
C++中的协程编程
开发语言·c++·算法
m0_748708051 天前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习1 天前
【算法——c/c++]
c语言·c++·算法
智码未来学堂1 天前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法