前言
本人是 xxs,搜索学的不多,文章有错误还请多多谅解。
由于本人做搜索题的时候被恶心到了,所以这里来恶心一下大家,怕恶心的也别走,有良心题。
暂时只有洛谷主题库和 CF 的一两道题,如果觉得有别的 OJ 的好题可以私信我哦。
题目难度在板块内整体递增,每个板块之间也保持整体递增(笔者认为)。
模板题 & 经典题
这里笔者选了一些个人认为非常经典的题目讲解,大家也可以看一看。
P1219
我们设第 行只有第 列有棋子,那么显然 为 的排列。然后 枚举排列即可,然后 T 飞了。
注意到题目还需要满足不在同一对角线。于是有 。枚举绝对值内的正负然后移项就能够得到 且 且 。用三个标记数组标记这三个值是否出现过即可。注意到 可能小于 ,可以通过 来把值域调整到 。
code
void dfs(vector<int> &a){
if(a.size() == n){
ans++;
if(ans <= 3){
for(int x : a){
cout << x << ' ';
}
cout << '\n';
}
}
for(int i = 1; i <= n; i++){
int x = a.size() + 1;
if(!vis[i] && !vis1[i + x] && !vis2[i - x + n]){
vis[i] = vis1[i + x] = vis2[i - x + n] = 1;
a.push_back(i);
dfs(a);
a.pop_back();
vis[i] = vis1[i + x] = vis2[i - x + n] = 0;
}
}
}P1434
直接搜索很容易死掉。但是我们发现,对于答案 ,显然前面的 是不会影响 的,因此我们考虑记忆化搜索。转移方程很简单,就是:
然后就是板子了。复杂度显然 。
code
int dfs(int x, int y){
if(f[x][y]) return f[x][t];
f[x][y] = 1;
for(int i = 0; i < 4; i++){
int xx = x + dx[i], yy = y + dy[i];
if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && a[xx][yy] < a[x][y]){
dfs(xx, yy);
f[x][y] = max(f[x][y], f[xx][yy] + 1);
}
}
return f[x][y];
}P1379
如题。
P4799
我们发现,当 的时候是可以直接搜索的。当 的时候是可以直接 的。但是 然后你发现你输了。
这里引入一个新东西:折半搜索(meet in the middle)。
我们可以统计出前 场比赛中总费用 的情况,对于后 场比赛同样的处理。假设会得到两个数组 ,然后枚举 ,对答案的贡献就是 中 的个数,排序之后二分处理即可。时间复杂度 。
code
void dfs(int x, int sum, int aa[], int &cnt){
if(sum > m) return ;
if(x > nn){
aa[++cnt] = sum;
return ;
}
dfs(x + 1, sum + a[x], aa, cnt);
dfs(x + 1, sum, aa, cnt);
}
nn = n >> 1;
dfs(1, 0, fa, cnt1);
nn = n;
dfs((n >> 1) + 1, 0, fb, cnt2);
sort(fa + 1, fa + cnt1 + 1);
for(int i = 1; i <= cnt2; i++)
ans += upper_bound(fa + 1, fa + cnt1 + 1, m - fb[i]) - fa - 1;P1074
考虑直接枚举每一个格子内填什么数。但是这样可能会超时,考虑一个很显然的优化:从剩余的数量少的某一行开始搜索,这样就能过了。
code
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int a[10][10], cnt[10], ans = -1, kkk[10];
bool row[10][10], col[10][10], g[10][10];
bool cmp(int i, int j){return kkk[i] < kkk[j];}
const int point[10][10] = {
自己想
};
const int where[10][10] = {
自己想
};
void dfs(int x){
if(x == 10){
int c = 0;
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= 9; j++){
自己想
}
}
ans = max(ans, c);
return ;
}
if(kkk[cnt[x]] == 0){
dfs(x + 1);
return ;
}
for(int i = 1; i <= 9; i++){
if(!a[cnt[x]][i]){
kkk[cnt[x]]--;
for(int j = 1; j <= 9; j++){
if(!row[cnt[x]][j] && !col[i][j] && !g[where[cnt[x]][i]][j]){
a[cnt[x]][i] = j;
row[cnt[x]][j] = col[i][j] = g[where[cnt[x]][i]][j] = 1;
dfs(x);
row[cnt[x]][j] = col[i][j] = g[where[cnt[x]][i]][j] = 0;
}
}
kkk[cnt[x]]++;
a[cnt[x]][i] = 0;
return ;
}
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
system("shutdown -p");//优化常数
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= 9; j++){
cin >> a[i][j];
if(a[i][j]){
自己想
}else kkk[i]++;
}
}
iota(cnt + 1, cnt + 9 + 1, 1);
sort(cnt + 1, cnt + 10, cmp);
dfs(1);
cout << ans;
return 0;
}P2324
IDA* 板子,不会的看八数码题解。和八数码一样,可以把估价函数设成去掉空格之后与目标状态位置相同棋不同的数量,用 map 去重。这里仅给出估价函数的代码实现。
code
struct node{
int a[6][6];//0 表示白,1 表示黑,2 表示空
node(){memset(a, 0, sizeof a);}
bool operator < (node o) const{
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 5; j++)
if(a[i][j] != o.a[i][j]) return a[i][j] < o.a[i][j];
return 0;
}
}s = node(), t = node();
int h(node o){
int cnt = 0;
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 5; j++) cnt += (t.a[i][j] != 2 && o.a[i][j] != t.a[i][j]);
return cnt;
}过渡题
这些题目是为了让大家了解搜索的一些 trick 并且避免大家被后面的恶心题恶心到进行的过渡。
P3956
搜索的思路很好想,void dfs(int x, int y, int c, bool f, int g) 分别表示:当前坐标,当前颜色,上一步是否使用第二种魔法,目前的价格。然后 T 飞了。
注意到搜到 之后后续的搜索树是一样的,因此考虑局部最优性剪枝(看我一百万年前写的垃圾)。令 表示走到 的最小代价。
code
void dfs(int x, int y, int c, bool f, int g){
if(g >= s[x][y]) return ;
if(x == m && y == m){
ans = min(ans, g);
return ;
}
v[x][y] = 1, s[x][y] = g;
for(int i = 0; i < 4; i++){
int xx = x + dx[i], yy = y + dy[i];
if(x >= 1 && x <= m && y >= 1 && y <= m && !v[xx][yy]){
if(!a[xx][yy] && !f){
dfs(xx, yy, c, 1, g + 2);
}else if(a[xx][yy]){
dfs(xx, yy, a[xx][yy], 0, g + (a[xx][yy] != c));
}
}
}
v[x][y] = 0;
}P1021
搜索没什么好讲的,用 dfs(int x, int lst, int mx, int sum) 表示选第 个,上一个选的(保证单调性优化),目前走到的最大值,目前的总和。然后每次添加一个数的时候直接再搜,T 飞了。
注意到这是一个完全背包问题,用 表示前 个数中取到 的最小次数,第一个 时 为答案。代码中压维了,注意判断边界。
code
int DP(int x, int s){
memset(dp, 0x3f, sizeof dp);
dp[0] = 0;
for(int i = 1; i <= n; i++)
for(int j = a[i]; j <= s; j++)
dp[j] = min(dp[j], dp[j - a[i]] + 1);
for(int i = 1; i <= s; i++)
if(dp[i] > n)
return i - 1;
return s;
}
void dfs(int x, int lst, int mx, int sum){
if(x > k){
if(mx > Ans){
Ans = mx;
for(int i = 1; i <= k; i++) ans[i] = a[i];
}
return ;
}
for(int i = lst + 1; i <= mx + 1; i++){
a[x] = i;
dfs(x + 1, i, DP(x, (sum + i) * n), sum + i);
}
}P2962
用 表示 能影响到的集合(状压)。枚举前 个数中每个灯开不开,用 map 记录最少的步数。然后枚举后一半,假设枚举到集合 ,最终影响到的开关灯集合为 ,答案就是最小的 mp[((1 << n) - 1) ^ t] + cnt,cnt 表示当前搜到的步数。(这其实就是一个折半搜索)
code
for(int i = 0; i < (1ll << (n >> 1)); i++){
int cnt = 0, t = 0;
for(int j = 0; j < (n >> 1); j++){
if((i >> j) & 1){
t ^= a[j];
cnt++;
}
}
if(mp.count(t)) mp[t] = min(mp[t], cnt);
else mp[t] = cnt;
}
for(int i = 0; i < (1ll << (n - (n >> 1))); i++){
int cnt = 0, t = 0;
for(int j = 0; j < (n - (n >> 1)); j++){
if((i >> j) & 1){
t ^= a[(n >> 1) + j];
cnt++;
}
}
if(mp.count(t ^ k)) ans = min(ans, cnt + mp[t ^ k]);
}恶心题
这些题目一般代码极其复杂或者剪枝极其多,容易让大家自闭。
人类智慧题
你绝对想不到做法。