版本比较
搜索板子
正文
增加 17 行删除 17 行
23 行未改动2425262728282930313233343536373637383939404142439 行未改动53545556575758596061626364656665666768686970717221 行未改动9495969798989910010010110210210310410510610610710810910911011111111211311411511611411511611711823 行未改动本质是嵌套循环枚举。
```cppbool vis[100];vector<int> q; vector<int> q;
void dfs(int step) { // step表示现在是第几层循环 if (step > k) { for (int i=0; i<k; i++) cout << q[i] << " "; cout << '\n'; return ; } }
for (int i=1; i<=n; i++) { // 选了数字i // 选了数字i if (vis[i] == 1) continue; q.push_back(i); vis[i] = 1; dfs(step+1);9 行未改动
每次数字范围都从上次选的数字开始
```cppvector<int> q; vector<int> q;
void dfs(int step, int last) { // step表示现在是第几层循环 if (step > k) { for (int i=0; i<k; i++) cout << q[i] << " "; cout << '\n'; return ; } }
for (int i=last+1; i<=n; i++) { // 选了数字i // 选了数字i q.push_back(i); dfs(step+1, i); q.pop_back(); }21 行未改动void bfs(int sx, int sy) { // 1.起点 入队 q.push(P {sx, sy, 1}); vis[sx][sy] = 1; // 标记起点已经访问 st[sx][sy] = 1; // 记录起点的步数 st[sx][sy] = 1; // 记录起点的步数 while (q.size()) { P now = q.front(); // 获取队首 P now = q.front(); // 获取队首 q.pop(); // 删除队首
for (int i=0; i<4; i++) { int nx = now.x + d[i][0]; int ny = now.y + d[i][1];
if (nx < 1 or nx > n or ny < 1 or ny > m) continue; if (mp[nx][ny] == '#' or vis[nx][ny]) continue;
vis[nx][ny] = 1; st[nx][ny] = now.st+1; // 记录每个点的最短步数 st[nx][ny] = now.st+1; // 记录每个点的最短步数 q.push({nx, ny, now.st+1}); } } // 处理终点答案的问题 }
// 处理终点答案的问题}```