【模拟赛】搜索综合
考试链接。
概述
| 题号 | 难度 | 主要知识点 | 其它知识点 |
|---|---|---|---|
| 橙 | BFS | 拆位、质数判断 | |
| 黄 | 棋盘 DFS | 小剪枝、路径输出 | |
| 绿 | DFS | STL 全排列 | |
| 蓝 | 棋盘 BFS | 图论 | |
| 橙 | 模拟 | 时间复杂度证明 |
个人评价:
- 第一、二题较易,是课上讲的内容。必须掌握,应该拿到满分。
- 第三题用到 DFS + 预处理全排列,用到
next_permutation函数,争取拿下。 - 第四题可能是最难的一道题,其实就是 BFS 棋盘最短路的改编版,注意细节以及审题即可。如果不会,也应该拿到部分分。
- 你是否注意到最后一道题最简单了?枚举(小优化)即可通过。
分数线:
- 分数线
1 :100+0+0+10+25=135 。 - 分数线
2 :100+100+0+28+25=253 。【越过这条线】 - 分数线
3 :100+100+100+28+50=378 。
\textbf{T1} 质数序列
前言
本题主要考察 BFS,注意多测清空。
预估难度:橙或黄。
题意
给定四位质数
题解
每次转移时,对于当前队首的质数 impossible。注意多测清空!!!
复杂度分析
这里定义值域上限
显然空间复杂度
每个质数最多被入栈出栈一次,
标程
#include <bits/stdc++.h>
using namespace std;
int T, a, b;
inline bool isPrime(int x) {
if (x < 2) return 0;
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return 0;
return 1;
}
const int N = 1e4 + 5;
int dis[N], p[5];
inline int Bfs(void) {
memset(dis, 0x3f, sizeof dis);
queue<int> q;
q.push(a);
dis[a] = 0;
while (!q.empty()) {
int x = q.front(); q.pop();
if (x == b) return dis[x];
p[0] = x % 10, p[1] = x / 10 % 10, p[2] = x / 100 % 10, p[3] = x / 1000;
int Pow = 1;
for (int k = 0; k <= 3; k++) {
for (int i = 0; i <= 9; i++) {
if (k == 3 && i == 0) continue;
int nx = x + (i - p[k]) * Pow;
if (!isPrime(nx)) continue;
if (dis[nx] <= dis[x] + 1) continue;
dis[nx] = dis[x] + 1;
q.push(nx);
}
Pow *= 10;
}
}
return -1;
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> T;
while (T--) {
cin >> a >> b;
int res = Bfs();
if (res == -1)
cout << "impossible" << endl;
else
cout << res << endl;
}
return 0;
}
\textbf{T2} 马的路径
前言
本题考查棋盘 DFS。不需要任何剪枝。
预估难度:黄。
题意
给定一个棋盘,马从左上角跳到右下角,不重复。已知每一行、每一列有多少个格子被经过。求所有可能的路径。
题解
DFS 到点
当
注:事实上,如果你想要骗分,你可以输出
标程
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
const int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int n, m, a[N], b[N];
bool vis[N][N];
struct route {
vector<int> vx, vy;
inline bool operator < (const route &cmp) const {
for (int i = 0; i < vx.size(); i++) {
if (vx[i] != cmp.vx[i])
return vx[i] < cmp.vx[i];
if (vy[i] != cmp.vy[i])
return vy[i] < cmp.vy[i];
}
return 0;
}
inline void push_back(int x, int y) {
vx.push_back(x);
vy.push_back(y);
}
inline void pop_back(void) {
vx.pop_back();
vy.pop_back();
}
inline void output(void) {
cout << vx.size() << ' ';
for (int i = 0; i < vx.size(); i++)
cout << vx[i] << ' ' << vy[i] << ' ';
cout << endl;
}
} now;
set<route> ans;
inline bool check(void) {
for (int i = 1; i <= n; i++)
if (a[i] != 0)
return 0;
for (int j = 1; j <= m; j++)
if (b[j] != 0)
return 0;
return 1;
}
void Dfs(int x, int y) {
vis[x][y] = 1;
a[x]--, b[y]--;
now.push_back(x, y);
if (x == n && y == m) {
if (check())
ans.insert(now);
vis[x][y] = 0;
a[x]++, b[y]++;
now.pop_back();
return;
}
for (int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
if (vis[nx][ny]) continue;
if (a[nx] == 0 || b[ny] == 0) continue;
Dfs(nx, ny);
}
vis[x][y] = 0;
a[x]++, b[y]++;
now.pop_back();
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int j = 1; j <= m; j++)
cin >> b[j];
Dfs(1, 1);
cout << ans.size() << endl;
for (route r : ans)
r.output();
return 0;
}
\textbf{T3} 货物运输
前言
本题改编自吃奶酪,但只考察 DFS(而不是状压 DP)。
预估难度:黄或绿。
题意
从
题解
首先,我们知道,一辆车从
否则剩下的那些货物装上去,占用体积而不运输,不如再来一趟车运输。
对于每个货物
那么对于货物
- 选择已有的一趟车,如果还能装得下,那么试图去运输。
- 新建一趟车,装上
x 。
注意:我们并不知道运输的顺序,只是记录运输哪些东西。
全部选完后,枚举每一辆车货物如何运输最优(全排列),将所有车的最小值相加,更新答案。
标程
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 12;
int n, m, ans = 1e18, v[N], idx[N], a[N][N];
int tot, status[N], sum[N];
inline int Dist(int s) {
int ans = 1e18;
int cnt = 0;
for (int i = 2; i <= n; i++)
if (s >> (i - 2) & 1)
idx[++cnt] = i;
do {
int res = a[1][idx[1]] + a[idx[cnt]][1];
for (int i = 2; i <= cnt; i++) {
res += a[idx[i - 1]][idx[i]];
if (res >= ans) break;
}
ans = min(ans, res);
} while (next_permutation(idx + 1, idx + cnt + 1));
return ans;
}
void Dfs(int step) {
if (step > n) {
int res = 0;
for (int i = 1; i <= tot; i++)
res += Dist(status[i]);
ans = min(ans, res);
return;
}
for (int i = 1; i <= tot; i++) {
if (sum[i] + v[step] > m) continue;
sum[i] += v[step];
status[i] ^= (1 << step - 2);
Dfs(step + 1);
sum[i] -= v[step];
status[i] ^= (1 << step - 2);
}
tot++;
status[tot] = (1 << step - 2), sum[tot] = v[step];
Dfs(step + 1);
tot--;
}
signed main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 2; i <= n; i++)
cin >> v[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
// for (int k = 1; k <= n; k++)
// for (int i = 1; i <= n; i++)
// for (int j = 1; j <= n; j++)
// a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
Dfs(2);
cout << ans << endl;
return 0;
}
\textbf{T4} 开灯
前言
本题考查 BFS 求解棋盘问题。
这道题是 USACO
预估难度:黄或绿。(这是个人评价。洛谷上评为蓝,有点虚。)
题意
给定一个棋盘,贝茜只能在亮着灯的格子走动,一些房间有其他房间灯的开关。贝茜从左上角(开始亮着灯)开始,只能上下左右走,求最终最多能开多少灯,而不是能走到多少个格子!(样例如果这样理解还可以通过。)
题解
首先建图,用一个二维 vector(定义:vector<pair<int, int>> graph[105][105])。
记录两个状态:
BFS 的过程如下:
- 开始时将
(1,1) 放入队列,标记vis 和on 状态。 - 每次取出队首
(x,y) ,将所有可以开的灯开开(标记on 状态)。 - 随后扫描所有的开开的灯,如果这个格子旁边有可以到达的格子(
vis 被标记过),那么这个格子可以到达,放入队列,标记vis 。
答案为
时间复杂度:每个点至多入队一次,每次转移
有更优的解法,请自行思考。
标程
#include <bits/stdc++.h>
#define r q.front()
using namespace std;
const int N = 105;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
int n, m, a, b, c, d;
struct node {
int x, y;
};
vector<node> v[N][N];
queue<node> q;
bool vis[N][N], on[N][N];
inline bool check(int x, int y) {
for (int i = 0; i < 4; i++) {
int lx = x + dx[i], ly = y + dy[i];
if (lx < 1 || lx > n || ly < 1 || ly > n) continue;
if (vis[lx][ly]) return 1;
}
return 0;
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a >> b >> c >> d;
v[a][b].push_back({c, d});
}
vis[1][1] = on[1][1] = 1;
q.push({1, 1});
while (!q.empty()) {
int x = r.x, y = r.y; q.pop();
for (auto i : v[x][y]) {
int nx = i.x, ny = i.y;
on[nx][ny] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (on[i][j] && !vis[i][j] && check(i, j)) {
vis[i][j] = 1;
q.push({i, j});
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
ans += on[i][j];
cout << ans << endl;
return 0;
}
\textbf{T5} 数学题
前言
骗人题,这么大的数据范围怎么可能是搜索。
本题考查枚举和时间复杂度计算。
预估难度:橙。
初步想法
看到向下取整,心生厌恶。
如果没有向下取整,即
于是,我们暴力枚举三个向下取整(范围:
然而……这样是
小优化
记
另外,在枚举的时候,我们需要保证
这样,复杂度立刻变成
你肯定不信,请看下面的证明:
复杂度证明
注:此段用了大量的数学知识,并不要求掌握,只需了解。
一共枚举的次数就等于满足
先证明几个引理:
引理
1 : 对于\forall x>0 ,\dfrac{x}{1+x}<\ln(1+x) 。
证明: 设f(x)=\dfrac{x}{1+x}-\ln(1+x) 。则\begin{aligned}f'(x)&=\left(\dfrac{x}{1+x}\right)'-\ln'(1+x)\\&=\dfrac{1\cdot(1+x)-x\cdot1}{(1+x)^2}-\dfrac{1}{1+x}\cdot1\\&=\dfrac{1-(1+x)}{(1+x)^2}\\&=-\dfrac{x}{(1+x)^2}\\&<0\end{aligned} 于是
f(x) 在(0,+\infty) 上单调递减。
所以f(x)<f(0)=\dfrac{0}{1+0}-\ln(1+0)=0 ,故左式小于右式,得证。
引理
2 : 对于任意正整数n\;(\ge2) ,\dfrac11+\dfrac12+\dfrac13+\dots+\dfrac1n<1+\ln n 。
证明: 由引理1 ,\dfrac 1m=\dfrac{\frac{1}{m-1}}{1+\frac{1}{m-1}}<\ln\left(1+\dfrac{1}{m-1}\right)=\ln m-\ln(m-1) ,对m=2,3,\dots,n 求和,得到\dfrac12+\dfrac13+\dots+\dfrac1n<\ln n ,两边同时加1 得到右侧式子。
引理
3. 对于n\in\mathbb N_+ ,满足i\times j\le n 的正整数对(i,j) 的个数不超过n(1+\ln n) 。
证明: 固定i ,有\left\lfloor\dfrac{n}{i}\right\rfloor 个满足条件的j ,所以一共有P=\sum_{i=1}^n\left\lfloor\dfrac{n}{i}\right\rfloor 个满足条件的正整数对。 由引理2 ,P\le\sum_{i=1}^n\dfrac{n}{i}=n\sum_{i=1}^n\dfrac{1}{i}<n(1+\ln n) ,即为结论。
再看原题,固定
这个数与
标程
#include <bits/stdc++.h>
#define int long long
using namespace std;
int cnt, a, b, c;
signed main(void) {
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cin >> a >> b >> c;
int m = min({a, b, c});
for (int i = 1; i <= m; i++)
for (int j = 1; i * j <= m; j++)
for (int k = 1; i * j * k <= m; k++) {
double x = sqrt(1.0 * b * c * i / a / j / k);
double y = sqrt(1.0 * a * c * j / b / k / i);
double z = sqrt(1.0 * a * b * k / c / i / j);
if ((int)floor(x) == i && (int)floor(y) == j && (int)floor(z) == k)
cnt++;
}
cout << cnt << endl;
return 0;
}