概述:《WiFi 设备网络规划》(题号
OD_1071)是 2026.04.19 考期华为 OD 机考 200分 真题,考查 深度优先搜索,采用 Core 核心代码模式(无需手写输入输出)。时间复杂度 最坏是指数级(每块空地最多 9 种放法),空间复杂度 O(n·m),下文给出具体推导步骤、边界排查与 Python、Java、C++ 实现。
2026年最新华为OD机考题库(按考点分类): 2026年最新华为OD机考题库
一、真题档案
| 维度 | 详情 |
|---|---|
| 题号 / 题名 | OD_1071 WiFi 设备网络规划 |
| 考期 / 分值 | 2026.04.19 / 200分 |
| 所属考点 | 深度优先搜索 |
| 做题模式 | Core 核心代码模式(无需手写输入输出) |
| 关联力扣 Hot100 | N 皇后 |
| 同类考点题目 | 云服务安全策略最优选择 / 智能家居模式调度优化 |
二、题目描述
WiFi 网络中,专业的网络规划不仅可以提升业务体验,还可以减少部署成本。
把办公区看作一个 n × m 的网格:
- 墙壁用字符
#表示,墙壁上不能放置 AP(WiFi 设备)。 - 空地用字符
.表示,空地上可以放置 AP。
每个 AP 覆盖以自身为中心、与网格相交的 3 × 3 正方形区域,包括自身位置、
上下左右以及四个对角线位置。任意两个 AP 的覆盖区域不能重叠;重叠检查包含墙壁格子。
请计算覆盖所有空地最少需要放置多少个 AP。如果无法在满足覆盖区域不重叠的条件下
覆盖所有空地,返回 -1。如果网格中没有空地,返回 0。
输入说明
参数 grid 是由字符串 “.” 和 “#” 组成的二维数组,行列均不超过 50。
输出说明
返回覆盖全部空地的最少 AP 数量;无法完成时返回 -1;没有空地时返回 0。
约束条件
0 <= n, m <= 50grid中的每个元素只能是.或#。- AP 只能放置在空地上。
- AP 的覆盖区域会在网格边界处截断。
- 两个 AP 的覆盖区域不能共享任何网格格子,墙壁格子也参与重叠判断。
- 输入允许为
[[]],此时返回0。
示例
样例1
输入
[
[".", ".", ".", "#", ".", ".", "."],
[".", ".", ".", "#", ".", ".", "."],
[".", ".", ".", "#", ".", ".", "."],
[".", ".", ".", "#", ".", ".", "."],
[".", ".", ".", "#", ".", ".", "."],
[".", ".", ".", "#", ".", ".", "."],
[".", ".", ".", "#", ".", ".", "."]
]
图示为:

输出
6
说明
图示为:(A 代表 AP 设备)

样例2
输入
[
[".", ".", "#", ".", "."],
[".", ".", "#", ".", "."],
[".", ".", "#", ".", "."],
[".", ".", "#", ".", "."],
[".", ".", "#", ".", "."]
]
图示为:

输出
4
说明
图示为:(A 代表 AP 设备)

样例3
输入
[
["."],
["."],
["."],
["."],
["."]
]
图示为:

输出
2
说明
图示为:(A 代表 AP 设备)

样例4
输入
[
[".", "#", ".", "#"],
["#", ".", "#", "."],
[".", "#", ".", "#"],
["#", ".", "#", "."]
]
图示为:

输出
-1
说明
图示为:无法部署,所有方案都存在覆盖范围重叠。
补充说明: 输入可以传入 [[]]。
三、解题思路
第一步:把规则翻译清楚
- AP 只能放在空地上,覆盖以它为中心的 3×3 区域(超出网格的部分不算);
- 任意两个 AP 的 3×3 区域不能有任何重叠,墙壁格子也算。两个 AP 的行号差和列号差都不超过 2 时,两个 3×3 一定有公共格子,这就是冲突;只要有一个方向的差 ≥ 3,就不冲突;
- 所有空地都要被覆盖,求最少放几个 AP,做不到返回 −1,没有空地返回 0。
因为覆盖区域不能重叠,每块空地必须恰好被一个 AP 覆盖。
第二步:先把关系算好
给所有空地编号。对每块空地 c,预先算出:
- covers[c]:AP 放在 c 时能覆盖哪些空地;
- 反过来,每块空地能被哪些位置的 AP 覆盖(就是它周围 3×3 里的空地);
- conflicts[c]:AP 放在 c 时,哪些位置就不能再放 AP(行列差都 ≤ 2 的空地)。
第三步:回溯搜索(DFS)
位置的组合太多,没有直接的公式,只能“试”。回溯就是有条理地试:做一个选择,接着往下试;试完了把这个选择撤销,再试下一个选择。
- 找一块还没被覆盖的空地。它迟早要被某个 AP 覆盖,而能覆盖它的 AP 只可能放在它周围 3×3 里,并且不能和已放的 AP 冲突;
- 对每个这样的位置:放上 AP,把它覆盖的空地标成“已覆盖”,把和它冲突的位置标成“不可用”,然后递归处理剩下的空地;
- 递归回来后,把这一步做的标记全部撤销,再试下一个位置;
- 所有空地都覆盖了,就得到一个方案,用它的 AP 数更新最优答案。
第四步:让搜索快起来的三个技巧
- 先处理选择最少的空地:第 1 步挑“还能用的候选位置最少”的那块空地。只剩 1 个选择时没得挑,直接放;剩 0 个说明这条路走不通,立刻返回。越早发现死路,白费的搜索越少;
- 先试覆盖得多的位置:候选按能覆盖的空地数从多到少试,更快找到一个不错的答案,后面的剪枝更有效;
- 估算下界,提前剪掉:每隔 3 行取一行(第 0、3、6……行,或第 1、4、7……行,或第 2、5、8……行),一个 AP 的 3 行最多碰到其中一行,在那一行最多覆盖 3 块空地。所以这些行至少还需要“每行剩余空地数 ÷ 3 向上取整”加起来那么多个 AP。列也一样。三种取法、行和列,取最大的作为下界。如果“已放的 AP 数 + 下界”已经不比当前最优答案少,这条路不可能更好,直接返回。
用样例 3 走一遍
网格是一列 5 块空地(第 0~4 行)。一开始每行剩 1 块、第 0 列剩 5 块,下界 = max(第 0、3 行各需 1 个 = 2,第 0 列需 ⌈5/3⌉ = 2) = 2。
| 递归层 | 已放 AP | 已覆盖 | 处理的空地 | 可选位置 | 选择 |
|---|---|---|---|---|---|
| 0 | 0 个 | 无 | 第 0 行(只能被第 0、1 行的 AP 覆盖,选择最少) | 第 1 行、第 0 行 | 先试第 1 行(覆盖 3 块) |
| 1 | 1 个 | 第 0、1、2 行 | 第 3 行 | 和第 1 行冲突的第 0~3 行都不能用,只剩第 4 行 | 放第 4 行 |
| 2 | 2 个 | 全部 | — | — | 得到方案,最优答案 = 2 |
回到第 0 层,准备试“AP 放第 0 行”之前检查:已放 0 个 + 下界 2 ≥ 最优 2,不可能更好,直接结束。答案 2。
样例 4:空地呈棋盘格交错,任意两块相邻的空地都在对方的 3×3 里,放一个 AP 就会和覆盖其他空地所需的 AP 冲突,所有分支都走不通,返回 −1。
四、高频 WA 易错点(边界排查)
- 重叠检查包含墙壁:两个 AP 的行差、列差都 ≤ 2 就冲突,哪怕重叠的格子是墙。
- AP 只能放在空地上,但覆盖范围可以包含墙壁;网格外的部分不算。
- 不能贪心地“看到第一个能放的位置就放”,必须回溯尝试所有可能。
- 回溯时要把本层做的修改(已覆盖、不可用)全部恢复,而且只恢复本层改过的。
- 没有空地(包括空网格、全是墙)时返回 0;一个方案都找不到时返回 −1。
五、时间与空间复杂度
时间复杂度:最坏是指数级(每块空地最多 9 种放法)。靠“先处理选择最少的空地”“冲突传播”和“行列下界”剪枝,实际搜索量很小。
空间复杂度:O(n·m),各种关系表、标记数组和递归栈。
六、参考代码实现
以下提供 Python、Java、C++ 核心实现(已按题目输入输出约定与边界用例自测)。
Python 实现
class Solution:
def wifiPlan(self, grid):
if not grid or not grid[0]:
return 0
rows, cols = len(grid), len(grid[0])
land_id = [[-1] * cols for _ in range(rows)]
lands = []
for row in range(rows):
for col in range(cols):
if grid[row][col] == ".":
land_id[row][col] = len(lands)
lands.append((row, col))
total = len(lands)
if total == 0:
return 0
# covers[c]:把 c 作为 AP 中心时能够覆盖的空地编号
covers = []
candidates_by_land = [[] for _ in range(total)]
for center, (center_row, center_col) in enumerate(lands):
covered_ids = []
for row in range(max(0, center_row - 1), min(rows, center_row + 2)):
for col in range(max(0, center_col - 1), min(cols, center_col + 2)):
index = land_id[row][col]
if index != -1:
covered_ids.append(index)
candidates_by_land[index].append(center)
covers.append(covered_ids)
# 两个中心的行距、列距都不超过 2 时,两个 3×3 覆盖区相交
conflicts = []
for center_row, center_col in lands:
values = []
for row in range(max(0, center_row - 2), min(rows, center_row + 3)):
for col in range(max(0, center_col - 2), min(cols, center_col + 3)):
index = land_id[row][col]
if index != -1:
values.append(index)
conflicts.append(values)
available = [True] * total
covered = [False] * total
row_remaining = [0] * rows
col_remaining = [0] * cols
for row, col in lands:
row_remaining[row] += 1
col_remaining[col] += 1
best = [total + 1]
def lower_bound():
# 每隔 3 行取一行:一个新 AP 最多服务其中一行里的 3 个剩余空地
bound = 0
for offset in range(3):
row_bound = sum(
(row_remaining[row] + 2) // 3
for row in range(offset, rows, 3)
)
col_bound = sum(
(col_remaining[col] + 2) // 3
for col in range(offset, cols, 3)
)
bound = max(bound, row_bound, col_bound)
return bound
def dfs(used):
bound = lower_bound()
if used + bound >= best[0]:
return
# 优先处理合法候选最少的未覆盖空地,尽早发现死路
target_candidates = None
for land_index in range(total):
if covered[land_index]:
continue
valid = [
candidate
for candidate in candidates_by_land[land_index]
if available[candidate]
]
if not valid:
return
if target_candidates is None or len(valid) < len(target_candidates):
target_candidates = valid
if len(valid) == 1:
break
if target_candidates is None:
best[0] = used
return
target_candidates.sort(key=lambda candidate: -len(covers[candidate]))
for candidate in target_candidates:
changed_available = []
for conflict in conflicts[candidate]:
if available[conflict]:
available[conflict] = False
changed_available.append(conflict)
changed_covered = []
for land_index in covers[candidate]:
covered[land_index] = True
row, col = lands[land_index]
row_remaining[row] -= 1
col_remaining[col] -= 1
changed_covered.append(land_index)
dfs(used + 1)
# 回溯时精确恢复本层修改的状态
for land_index in changed_covered:
covered[land_index] = False
row, col = lands[land_index]
row_remaining[row] += 1
col_remaining[col] += 1
for conflict in changed_available:
available[conflict] = True
if used + bound >= best[0]:
return
dfs(0)
return best[0] if best[0] <= total else -1
Java 实现
class Solution {
private int rows, cols, total, best;
private int[] landRows, landCols, rowRemaining, colRemaining;
private int[][] covers, conflicts, candidatesByLand;
private boolean[] available, covered;
private int lowerBound() {
int bound = 0;
for (int offset = 0; offset < 3; offset++) {
int current = 0;
for (int row = offset; row < rows; row += 3) {
current += (rowRemaining[row] + 2) / 3;
}
bound = Math.max(bound, current);
current = 0;
for (int col = offset; col < cols; col += 3) {
current += (colRemaining[col] + 2) / 3;
}
bound = Math.max(bound, current);
}
return bound;
}
private void dfs(int used) {
int bound = lowerBound();
if (used + bound >= best) return;
// 优先处理可用 AP 候选最少的未覆盖空地
int[] targetCandidates = null;
int targetCount = total + 1;
for (int land = 0; land < total; land++) {
if (covered[land]) continue;
int count = 0;
for (int candidate : candidatesByLand[land]) {
if (available[candidate]) count++;
}
if (count == 0) return;
if (count < targetCount) {
targetCount = count;
targetCandidates = new int[count];
int write = 0;
for (int candidate : candidatesByLand[land]) {
if (available[candidate]) targetCandidates[write++] = candidate;
}
if (count == 1) break;
}
}
if (targetCandidates == null) {
best = used;
return;
}
// 覆盖空地更多的候选优先,尽快得到较好的上界
for (int i = 1; i < targetCandidates.length; i++) {
int value = targetCandidates[i];
int j = i - 1;
while (j >= 0 && covers[targetCandidates[j]].length < covers[value].length) {
targetCandidates[j + 1] = targetCandidates[j--];
}
targetCandidates[j + 1] = value;
}
for (int candidate : targetCandidates) {
int[] changedAvailable = new int[conflicts[candidate].length];
int changedAvailableCount = 0;
for (int conflict : conflicts[candidate]) {
if (available[conflict]) {
available[conflict] = false;
changedAvailable[changedAvailableCount++] = conflict;
}
}
for (int land : covers[candidate]) {
// 可用候选不与既有 AP 冲突,因此这些空地尚未被覆盖
covered[land] = true;
rowRemaining[landRows[land]]--;
colRemaining[landCols[land]]--;
}
dfs(used + 1);
// 回溯恢复本层修改
for (int land : covers[candidate]) {
covered[land] = false;
rowRemaining[landRows[land]]++;
colRemaining[landCols[land]]++;
}
for (int i = 0; i < changedAvailableCount; i++) {
available[changedAvailable[i]] = true;
}
if (used + bound >= best) return;
}
}
public int wifiPlan(String[][] grid) {
if (grid.length == 0 || grid[0].length == 0) return 0;
rows = grid.length;
cols = grid[0].length;
int[][] landId = new int[rows][cols];
for (int[] row : landId) java.util.Arrays.fill(row, -1);
int capacity = rows * cols;
landRows = new int[capacity];
landCols = new int[capacity];
total = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (".".equals(grid[row][col])) {
landId[row][col] = total;
landRows[total] = row;
landCols[total] = col;
total++;
}
}
}
if (total == 0) return 0;
java.util.List<Integer>[] coverLists = new java.util.ArrayList[total];
java.util.List<Integer>[] conflictLists = new java.util.ArrayList[total];
java.util.List<Integer>[] candidateLists = new java.util.ArrayList[total];
for (int i = 0; i < total; i++) {
coverLists[i] = new java.util.ArrayList<>();
conflictLists[i] = new java.util.ArrayList<>();
candidateLists[i] = new java.util.ArrayList<>();
}
for (int center = 0; center < total; center++) {
int centerRow = landRows[center], centerCol = landCols[center];
for (int row = Math.max(0, centerRow - 1); row < Math.min(rows, centerRow + 2); row++) {
for (int col = Math.max(0, centerCol - 1); col < Math.min(cols, centerCol + 2); col++) {
int land = landId[row][col];
if (land != -1) {
coverLists[center].add(land);
candidateLists[land].add(center);
}
}
}
// 行距和列距都不超过 2 的 AP 覆盖区域会发生重叠
for (int row = Math.max(0, centerRow - 2); row < Math.min(rows, centerRow + 3); row++) {
for (int col = Math.max(0, centerCol - 2); col < Math.min(cols, centerCol + 3); col++) {
int conflict = landId[row][col];
if (conflict != -1) conflictLists[center].add(conflict);
}
}
}
covers = toArrays(coverLists);
conflicts = toArrays(conflictLists);
candidatesByLand = toArrays(candidateLists);
available = new boolean[total];
java.util.Arrays.fill(available, true);
covered = new boolean[total];
rowRemaining = new int[rows];
colRemaining = new int[cols];
for (int land = 0; land < total; land++) {
rowRemaining[landRows[land]]++;
colRemaining[landCols[land]]++;
}
best = total + 1;
dfs(0);
return best <= total ? best : -1;
}
private int[][] toArrays(java.util.List<Integer>[] lists) {
int[][] result = new int[lists.length][];
for (int i = 0; i < lists.length; i++) {
result[i] = new int[lists[i].size()];
for (int j = 0; j < lists[i].size(); j++) result[i][j] = lists[i].get(j);
}
return result;
}
}
C++ 实现
class Solution {
int rows = 0, cols = 0, total = 0, best = 0;
vector<pair<int, int>> lands;
vector<vector<int>> covers, conflicts, candidatesByLand;
vector<char> available, covered;
vector<int> rowRemaining, colRemaining;
int lowerBound() {
int bound = 0;
for (int offset = 0; offset < 3; ++offset) {
int current = 0;
for (int row = offset; row < rows; row += 3) {
current += (rowRemaining[row] + 2) / 3;
}
bound = max(bound, current);
current = 0;
for (int col = offset; col < cols; col += 3) {
current += (colRemaining[col] + 2) / 3;
}
bound = max(bound, current);
}
return bound;
}
void dfs(int used) {
int bound = lowerBound();
if (used + bound >= best) return;
// 优先选择可用 AP 候选最少的未覆盖空地
vector<int> targetCandidates;
int targetCount = total + 1;
for (int land = 0; land < total; ++land) {
if (covered[land]) continue;
vector<int> valid;
for (int candidate : candidatesByLand[land]) {
if (available[candidate]) valid.push_back(candidate);
}
if (valid.empty()) return;
if ((int)valid.size() < targetCount) {
targetCount = valid.size();
targetCandidates = std::move(valid);
if (targetCount == 1) break;
}
}
if (targetCount == total + 1) {
best = used;
return;
}
sort(targetCandidates.begin(), targetCandidates.end(), [&](int left, int right) {
return covers[left].size() > covers[right].size();
});
for (int candidate : targetCandidates) {
vector<int> changedAvailable;
for (int conflict : conflicts[candidate]) {
if (available[conflict]) {
available[conflict] = false;
changedAvailable.push_back(conflict);
}
}
vector<int> changedCovered;
for (int land : covers[candidate]) {
// 可用候选与已放置 AP 不冲突,因此这些空地尚未被覆盖
covered[land] = true;
int row = lands[land].first;
int col = lands[land].second;
--rowRemaining[row];
--colRemaining[col];
changedCovered.push_back(land);
}
dfs(used + 1);
// 回溯恢复覆盖状态和候选状态
for (int land : changedCovered) {
covered[land] = false;
int row = lands[land].first;
int col = lands[land].second;
++rowRemaining[row];
++colRemaining[col];
}
for (int conflict : changedAvailable) available[conflict] = true;
if (used + bound >= best) return;
}
}
public:
int wifiPlan(vector<vector<string>> grid) {
if (grid.empty() || grid[0].empty()) return 0;
rows = grid.size();
cols = grid[0].size();
vector<vector<int>> landId(rows, vector<int>(cols, -1));
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
if (grid[row][col] == ".") {
landId[row][col] = lands.size();
lands.push_back({row, col});
}
}
}
total = lands.size();
if (total == 0) return 0;
covers.assign(total, {});
conflicts.assign(total, {});
candidatesByLand.assign(total, {});
for (int center = 0; center < total; ++center) {
int centerRow = lands[center].first;
int centerCol = lands[center].second;
for (int row = max(0, centerRow - 1); row < min(rows, centerRow + 2); ++row) {
for (int col = max(0, centerCol - 1); col < min(cols, centerCol + 2); ++col) {
int land = landId[row][col];
if (land != -1) {
covers[center].push_back(land);
candidatesByLand[land].push_back(center);
}
}
}
// 行距和列距都不超过 2 的 AP 覆盖区域会发生重叠
for (int row = max(0, centerRow - 2); row < min(rows, centerRow + 3); ++row) {
for (int col = max(0, centerCol - 2); col < min(cols, centerCol + 3); ++col) {
int conflict = landId[row][col];
if (conflict != -1) conflicts[center].push_back(conflict);
}
}
}
available.assign(total, true);
covered.assign(total, false);
rowRemaining.assign(rows, 0);
colRemaining.assign(cols, 0);
for (const auto& land : lands) {
int row = land.first;
int col = land.second;
++rowRemaining[row];
++colRemaining[col];
}
best = total + 1;
dfs(0);
return best <= total ? best : -1;
}
};
七、同类考点巩固与练习参考
- 边界自测清单:提交前建议重点手写验证极小规模(长度为 1 或空输入)、重复元素、边界等号条件以及大数溢出等情况,避免只过题面样例。
- 同类题连续巩固:掌握本题后,可结合同类考点题(云服务安全策略最优选择 / 智能家居模式调度优化)及力扣 Hot100 原题(N 皇后)进一步巩固「深度优先搜索」题型。
- 练习参考:如需在线提交评测,可参考题目练习页:OD_1071 WiFi 设备网络规划。
八、常见问题(FAQ)
Q1:《WiFi 设备网络规划》在华为 OD 机考里属于什么难度和题型?
该题在 2026.04.19 考期分值为 200分,核心考点为 深度优先搜索,判题模式为 Core 核心代码模式(无需手写输入输出)。
Q2:本地样例通过但提交报 WA(答案错误)通常怎么排查?
建议优先核对本文第四节的边界坑点,依次检查:下标从 0 还是 1 开始、比较条件是严格大于/小于还是包含等于、重复值处理、极端最小/最大数据范围,以及输出格式中的空格或换行符。
Q3:Core 核心代码模式和 ACM 模式在编写代码时有什么区别?
Core 核心代码模式只需实现给定的类和方法签名并按要求返回结果,无需手写标准输入解析与打印;ACM 模式则需要自行从标准输入读取多行或不定长数据并按约定格式输出,实际机考以题目页面给出的模板为准。
1万+



被折叠的 条评论
为什么被折叠?



