深度优先搜索的原理就是递归搜索答案,而递归的本质就是数学归纳法。所有在解决具体问题的时候,可以多想一下数学归纳法。
1、奶牛选美
题意大概是,找出两个连通块之间的最短距离。(移动方向只有上、下、左、右四个方向)
思路:先观察题目数据范围为0~50,数据量太小了,所以考虑枚举任意两个连通快的点,然后计算距离,最后输出最小值。
所以问题变成了找出两个连通块,并记录两个块中所有点的坐标,然后计算距离即可。
找连通块采用dfs搜索即可。
#include <iostream>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
char map[55][55];
vector<PII> points[2];
int n, m, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void dfs(int a, int b, vector<PII> &p)
{
p.push_back({a, b});
map[a][b] = '.';
for (int i = 0; i < 4; i ++)
{
int tx = a + dx[i], ty = b + dy[i];
if (tx >= 0 && tx < n && ty >= 0 && ty < m && map[tx][ty] == 'X')
dfs(tx, ty, p); // 符合条件就搜索
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
cin >> map[i][j];
int cnt = 0;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
if (map[i][j] == 'X')
dfs(i, j, points[cnt ++]); // 找连通块
int res = 100;
for (auto &a: points[0])
for (auto &b: points[1])
res = min(res, abs(a.x - b.x) + abs(a.y - b.y));
cout << res - 1 << endl;
return 0;
}
2、树的重心
题意:给定一颗树,树中包含 n 个结点(编号 1∼n)和 n−1 条无向边。请你找到树的重心,并输出将重心删除后,剩余各个连通块中点数的最大值。
(重心定义:重心是指树中的一个结点,如果将这个点删除后,剩余各个连通块中点数的最大值最小,那么这个节点被称为树的重心。)
思路:模板题,考察的就是图的遍历。
知识点:如何建图,如何灵活运用dfs得出答案(数学归纳法)
h[1] | 2 | 3 | 4 | 5 |
h[2] | 3 | 4 | 5 | 6 |
... | ... | ... | ... | ... |
h[n] | 7 | 8 | 9 | 10 |
h[i]存放的就是哪些点与i号点相连,可以用vector来存,也可以用数组模拟单链表来存。
如果有权重等其他性质,可以存结构体。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int n, h[N], e[2 * N], ne[2 * N], idx = 1, ans = N;
bool st[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
// 以u为根节点的子树节点数 (数学归纳法)
int dfs(int u)
{
st[u] = true;
int res = 0, sum = 1;
for (int i = h[u]; i != -1; i = ne[i])
{
// 计算以每个子节点为根节点的子树节点数
int j = e[i];
if (!st[j])
{
int s = dfs(j);
res = max(res, s);
sum += s;
}
}
res = max(res, n - sum);
ans = min(ans, res);
return sum;
}
int main()
{
cin >> n;
memset(h, -1, sizeof h);
for (int i = 0; i < n - 1; i ++)
{
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
dfs(1);
cout << ans << endl;
return 0;
}
3、大臣的旅费
题意求一个无向图的直径。
思路:别的dalao写的证明
知识点:如何求树的直径,就是做两次dfs:1、求任意一点到哪一个点的距离最远,记该最远点为点p;2、求哪个点到点p的距离最远,且最远距离为树的直径。
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
int n, dist[N];
bool st[N];
struct Edge
{
int idx, w;
};
vector<Edge> h[N];
void dfs(int u, int distance)
{
dist[u] = distance;
st[u] = true;
for (auto node: h[u])
if (!st[node.idx])
dfs(node.idx, distance + node.w);
}
int main()
{
cin >> n;
for (int i = 0; i < n - 1; i ++)
{
int a, b, c;
cin >> a >> b >> c;
h[a].push_back({b, c});
h[b].push_back({a, c});
}
memset(st, false, sizeof st);
dfs(1, 0);
int u = 1;
for (int i = 1; i <= n; i ++)
if (dist[i] > dist[u])
u = i;
memset(st, false, sizeof st);
dfs(u, 0);
for (int i = 1; i <= n; i ++)
if (dist[i] > dist[u])
u = i;
cout << dist[u] * 10 + dist[u] * (dist[u] + 1ll) / 2 << endl;
return 0;
}
4、迷宫
题意就是走迷宫。
知识点:dfs或者bfs,处理好边界信息,如果有多组数据,用memset处理好bool数组。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 105;
int t, n, x1, y1, x2, y2;
char map[N][N];
bool st[N][N], flag = false;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
void dfs(int x, int y)
{
if (x == x2 && y == y2 && map[x][y] != '#')
{
flag = true;
return;
}
for (int i = 0; i < 4; i ++)
{
int tx = x + dx[i], ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= n || map[tx][ty] == '#' || st[tx][ty] == true) continue;
st[tx][ty] = true;
dfs(tx, ty);
}
return ;
}
int main()
{
cin >> t;
while (t --)
{
cin >> n;
memset(st, false, sizeof st);
flag = false;
for (int i = 0; i < n; i ++) cin >> map[i];
cin >> x1 >> y1 >> x2 >> y2;
dfs(x1, y1);
if (flag && map[x1][y1] != '#') puts("YES");
else puts("NO");
}
return 0;
}
5、重头戏!——扫雷
题目有些长,这里讲一下优化的思路。
思路:读完之后发现就是个图的遍历问题,但在考虑建图的时候出现了问题,如果要建地雷和排雷火箭的图,数据量在5e4 * 5e4也就是1e9左右,不太现实,所以考虑只存地雷,然后枚举排雷火箭周围的地雷呢?极端情况下,一个地雷最多能和旁边300多个雷建立边(半径取最大10,面积大概300多,里面全填满雷,都可以建立边),数据量3e7左右也有些大。
所以考虑不建图,通过枚举排雷火箭然后枚举排雷火箭能引爆哪些地雷,所以为了快速判断哪些地雷是否被引爆,考虑使用哈希表。
又考虑到题目中说,同一个的点可以放置多个雷或排雷火箭,所以在哈希的时候再对地雷进行合并。
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 50010, M = 999997;
int n, m;
struct Circle
{
int x, y, r;
}cir[N];
LL h[M]; // 存key值
int id[M]; // 哈希后 点的编号
bool st[M]; // 点是否被访问过
// 使用哈希表目的:因为图不好建立,所以不建图,使用哈希表来快速判断这个点是否被访问
LL get_key(int x, int y)
{
return x * 1000000001ll + y;
}
int find(int x, int y)
{
LL key = get_key(x, y);
int t = (key % M + M) % M;
while (h[t] != -1 && h[t] != key)
if ( ++ t == M)
t = 0;
return t;
}
int sqr(int x)
{
return x * x;
}
void dfs(int x, int y, int r)
{
st[find(x, y)] = true;
for (int i = x - r; i <= x + r; i ++ )
for (int j = y - r; j <= y + r; j ++ )
if (sqr(i - x) + sqr(j - y) <= sqr(r))
{
int t = find(i, j);
if (id[t] && !st[t])
dfs(i, j, cir[id[t]].r);
}
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++ )
{
int x, y, r;
scanf("%d%d%d", &x, &y, &r);
cir[i] = {x, y, r};
int t = find(x, y);
if (h[t] == -1) h[t] = get_key(x, y); // 将点(x, y)映射到1~M
if (!id[t] || cir[id[t]].r < r) // 再将点映射到1~n 如果重合则合并
id[t] = i;
}
while (m -- )
{
int x, y, r;
scanf("%d%d%d", &x, &y, &r);
for (int i = x - r; i <= x + r; i ++ )
for (int j = y - r; j <= y + r; j ++ )
if (sqr(i - x) + sqr(j - y) <= sqr(r))
{
int t = find(i, j);
if (id[t] && !st[t]) // 判断枚举的这个点是不是雷 如果是雷则必须要没被访问过
dfs(i, j, cir[id[t]].r);
}
}
int res = 0;
for (int i = 1; i <= n; i ++ )
if (st[find(cir[i].x, cir[i].y)])
res ++ ;
printf("%d\n", res);
return 0;
}