这道题看起来像在一个房间 grid 里移动,但真正难的地方不是上下左右走格子,而是理解“格子里的数字”其实在告诉你下一批可能到达的位置。
如果你把每个格子当成一个 graph node,那么这道题就是一个 reachability problem:从左上角 (1, 1) 出发,能不能最终到达右下角 (M, N)。关键是这些边不需要提前全部画出来,我们可以在 BFS 过程中按需要展开。
本题在 CCC DSA 地图里的位置
- Level: Junior J5 / Senior S2 transition
- Pattern: Implicit Graph Search, BFS, Product-to-Cells Index
- Prerequisite: queues, visited sets/arrays, grid coordinates, dictionaries or indexed lists, factor/product reasoning
- What this teaches: 图不一定会直接给出 adjacency list;有时要从题目规则中即时生成边。
- Where it appears again: graph reachability, state graphs, Dijkstra, BFS with preprocessing
下一步推荐
如果你已经理解 Escape Room,可以继续看:
- 相关 CP Weekly 真题: CCC 2024 J5 Harvest Waterloo (grid flood fill)、CCC 2018 J5 Choose Your Own Adventure(directed graph reachability,coming soon)、CCC 2013 S4 Who is Taller(graph reachability,coming soon)
- 相关 Python / C++ 基础:queues/deque、2D arrays、sets/visited arrays、dictionaries/maps、functions for graph expansion
- 相关 DSA 专题:CCC Grid Problems: from simulation to graph search(planned)、CCC Senior Graph Toolkit(planned)
题目要我们做什么
房间是一个 M x N 的 grid。每个格子 (r, c) 里有一个正整数。
你从左上角 (1, 1) 开始,目标是到达右下角 (M, N)。
移动规则比较特别:
如果当前格子的数字是 x,
你可以跳到任何满足 a * b = x 的格子 (a, b)。
例如,一个格子的数字是 6,理论上可以跳到:
(1, 6), (2, 3), (3, 2), (6, 1)
但只有在这些 row 和 column 都在 grid 范围内时,才是真正可跳的位置。
最后输出:
yes
如果可以逃出;否则输出:
no
初学者容易卡在哪里
很多同学第一眼会把它当成普通 grid problem,然后开始想:
从这个格子往上、下、左、右走?
但这题不是四方向移动。你能跳到哪里,只由当前格子的 value 决定。
第二个常见误区是:每到一个格子,就现场分解当前数字 x,枚举所有 factor pair。
例如:
x = 12
factor pairs: (1,12), (2,6), (3,4), ...
这个做法可以写出来,但如果很多格子反复出现同一个 value,我们会重复做大量工作。
这道题真正要练的是一个更竞赛化的思维:
不要问“这个数字有哪些因子”。
先反过来建索引:哪些格子的 row * col 等于这个数字?
核心算法概念
这道题的核心是 implicit graph search。
我们可以把每个格子看成一个节点:
node = (r, c)
如果格子 (r, c) 的值是 x,那么它有 directed edges 指向所有满足:
a * b = x
的格子 (a, b)。
因为我们只需要知道“能不能到达终点”,不需要最短路径长度,所以 BFS 或 DFS 都可以。这里使用 BFS,写法稳定,也方便控制 visited。
数据结构设计
最重要的数据结构是:
product_to_cells[p] = 所有满足 r * c = p 的格子
例如在一个 3 x 4 grid 里:
product_to_cells[3] = [(1, 3), (3, 1)]
product_to_cells[4] = [(1, 4), (2, 2)]
product_to_cells[12] = [(3, 4)]
这样,当我们站在一个 value 为 x 的格子时,不用现场找因子,直接查:
product_to_cells[x]
就能得到所有合法目的地。
还需要两个 visited 结构:
visited[r][c] = 这个格子是否已经进入过 BFS queue
expanded[x] = value x 对应的目的地列表是否已经展开过
visited 防止同一个 cell 反复进队。
expanded 是这题的关键优化。如果多个已访问格子的 value 都是 12,我们只需要展开一次 product_to_cells[12]。第二次遇到 value 12 时,直接跳过,因为所有能由 12 到达的位置已经处理过了。
算法步骤
- 读入
M,N和整个 grid。 - 建立
product_to_cells:
- 对每个合法坐标
(r, c),计算p = r * c。 - 把
(r, c)放进product_to_cells[p]。
- 从
(1, 1)开始 BFS。 - 每次弹出当前格子
(r, c):
- 如果它是
(M, N),输出yes。 - 读取当前格子的值
x = grid[r][c]。 - 如果
x不在1..M*N之间,它不可能对应任何合法格子,跳过。 - 如果
x已经展开过,跳过。 - 否则遍历
product_to_cells[x],把没访问过的格子加入 BFS queue。
- 如果 BFS 结束仍然没有到达
(M, N),输出no。
Python 解法
from collections import deque
def solve():
M = int(input())
N = int(input())
grid = [[0] * (N + 1)]
for _ in range(M):
grid.append([0] + list(map(int, input().split())))
max_product = M * N
product_to_cells = [[] for _ in range(max_product + 1)]
for r in range(1, M + 1):
for c in range(1, N + 1):
product_to_cells[r * c].append((r, c))
visited = [[False] * (N + 1) for _ in range(M + 1)]
expanded = [False] * (max_product + 1)
q = deque()
q.append((1, 1))
visited[1][1] = True
while q:
r, c = q.popleft()
if r == M and c == N:
print("yes")
return
value = grid[r][c]
if value < 1 or value > max_product:
continue
if expanded[value]:
continue
expanded[value] = True
for nr, nc in product_to_cells[value]:
if not visited[nr][nc]:
visited[nr][nc] = True
q.append((nr, nc))
print("no")
solve()
这份代码里有一个细节很重要:
expanded[value] = True
它不是标记一个格子,而是标记“这个乘积 value 对应的所有目的地已经展开过”。这是避免重复扫描的关键。
C++ 解法
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int M, N;
cin >> M >> N;
vector<vector<int>> grid(M + 1, vector<int>(N + 1));
for (int r = 1; r <= M; r++) {
for (int c = 1; c <= N; c++) {
cin >> grid[r][c];
}
}
int maxProduct = M * N;
vector<vector<pair<int, int>>> productToCells(maxProduct + 1);
for (int r = 1; r <= M; r++) {
for (int c = 1; c <= N; c++) {
productToCells[r * c].push_back({r, c});
}
}
vector<vector<bool>> visited(M + 1, vector<bool>(N + 1, false));
vector<bool> expanded(maxProduct + 1, false);
queue<pair<int, int>> q;
q.push({1, 1});
visited[1][1] = true;
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
if (r == M && c == N) {
cout << "yes\n";
return 0;
}
int value = grid[r][c];
if (value < 1 || value > maxProduct) {
continue;
}
if (expanded[value]) {
continue;
}
expanded[value] = true;
for (auto [nr, nc] : productToCells[value]) {
if (!visited[nr][nc]) {
visited[nr][nc] = true;
q.push({nr, nc});
}
}
}
cout << "no\n";
return 0;
}
C++ 版本和 Python 版本的逻辑完全一致:
productToCells是乘积到坐标列表的索引。visited标记已经进过 queue 的格子。expanded标记某个 value 是否已经展开过。
复杂度分析
建立 product_to_cells 时,每个格子处理一次:
O(MN)
BFS 中,每个格子最多进队一次。每个 product 的目的地列表最多展开一次。所有目的地列表加起来一共也是 MN 个坐标。
所以总时间复杂度是:
O(MN)
空间复杂度主要来自 grid、visited 和 product_to_cells:
O(MN)
常见错误
- 把题目误解成上下左右四方向移动。
- 忘记坐标是从
1开始编号,不是从0开始。 - 每次都重新枚举因子,导致重复工作太多。
- 只用
visited cell,但没有用expanded value,同一个 value 会被反复展开。 - 没有处理
value > M * N的情况,结果访问 product 索引时越界。 - 以为一定要找最短路径。其实这题只问能不能到达。
复习问题
- 为什么这道题可以看成 graph reachability?
product_to_cells[x]存的是什么?- 为什么
expanded[value]比只用visited[r][c]更快? - 如果某个格子的 value 大于
M * N,为什么它不能跳到任何合法格子? - BFS 和 DFS 在这题里都可以吗?为什么?
有问题或反馈?
如果你对本文有疑问,或希望提出改进建议,欢迎发送邮件告诉我们。
