版本比较
2.6讲义
正文
增加 22 行删除 22 行
17 行未改动18192021222223242526242526272829303031323334353637383839404142434445464745464748495050515253542 行未改动575859606161626364656 行未改动727374757676777879798081828383848586874 行未改动9293949596969798991001011011021031041051051061071081091101091101111121131144 行未改动1191201211221231231241251261275 行未改动13313413513613713713813914014114 行未改动1561571581591601601611621631643 行未改动17 行未改动## 奇怪的电梯
```cpp/*1. 对什么东西选和不选,这次按上还是按下 1. 对什么东西选和不选,这次按上还是按下2. 上和下有什么影响 假设现在在第x层, 增加k[x]层,或者减少 k[x] 3. 什么时候结束,到达b层楼结束 4. 答案怎么处理,取最小的按键次数 假设现在在第x层, 增加k[x]层,或者减少 k[x]3. 什么时候结束,到达b层楼结束4. 答案怎么处理,取最小的按键次数*/
#include <bits/stdc++.h>#define int long long #define int long longusing namespace std;
int n, a, b, res = INT_MAX;int k[25], vis[25];
void dfs(int x, int cnt) { if (x < 1 or x > n or vis[x] == 1) { // 可行性剪枝 // 可行性剪枝 return ; } if (x == b) { res = min(res, cnt); return ; } vis[x] = 1; // 标记当前楼层已经走过
vis[x] = 1; // 标记当前楼层已经走过
dfs(x+k[x], cnt+1); dfs(x-k[x], cnt+1);
vis[x] = 0;}
signed main() {2 行未改动
cin >> n >> a >> b; for (int i=1; i<=n; i++) cin >> k[i]; dfs(a, 0); if (res == INT_MAX) cout << -1; if (res == INT_MAX) cout << -1; else cout << res; return 0;}```6 行未改动是一个合法方案。
1. 对n个单词选或者不选2. 选单词,它里面的字母出现次数+1 2. 选单词,它里面的字母出现次数+1 不选单词 ,无事发生3. 什么时候结束?n个物品处理完4. 答案要处理什么?所有小写字母都出现过 4. 答案要处理什么?所有小写字母都出现过*/
#include <bits/stdc++.h>#define int long long #define int long longusing namespace std;
int n, res;string s[20];4 行未改动 // 怎么判断答案? int sum = 0; for (int i='a'; i<='z'; i++) { if (t[i] > 0) sum++; } } if (sum == 26) res++; return ; } // s[step] string q = s[step]; // 字符串q中的每个字母出现次数增加 string q = s[step]; // 字符串q中的每个字母出现次数增加 for (int i=0; i<q.size(); i++) { t[q[i]]++; } dfs(step+1); // 选 dfs(step+1); // 选 for (int i=0; i<q.size(); i++) { t[q[i]]--; } dfs(step+1); // 不选
dfs(step+1); // 不选}
signed main() { ios::sync_with_stdio(0);4 行未改动 cin >> s[i]; } dfs(1); cout << res;
return 0;}```
5 行未改动2. 选的影响:木棍总和增加,最大值可能会变化 不选的影响:不变
3. 什么时候结束:n个物品处理完结束4. 怎么处理答案: 选中的木棍数量大于等于3,并且总长度 > 最大值的两倍 4. 怎么处理答案: 选中的木棍数量大于等于3,并且总长度 > 最大值的两倍*/#include <bits/stdc++.h>using namespace std;
14 行未改动 dfs(step+1, sum, maxn, cnt);}
int main() {
cin >> n; for (int i=1; i<=n; i++) cin >> a[i]; dfs(1, 0, 0, 0); cout << res;3 行未改动