【CF1349C Orac and Game of Life】题解
本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/16297018.html
题目链接
题目
- 给定第 个时刻的 的 矩阵。
- 每过一个时刻, 矩阵都会发生如下的变化:
- 考虑第 行第 列的格子。若其上下左右四个方向中相邻的格子存在与其数字相同的格子,则此格子在下一个时刻会变成另一个数字( 变 , 变 )。
- 有 次询问。每次询问你在第 个时刻第 行第 列的格子上的数字是什么。
- ,,,,。
Please notice the unusual memory limit of this problem.
Orac likes games. Recently he came up with the new game, “Game of Life”.
You should play this game on a black and white grid with $ n $ rows and $ m $ columns. Each cell is either black or white.
For each iteration of the game (the initial iteration is $ 0 $ ), the color of each cell will change under the following rules:
- If there are no adjacent cells with the same color as this cell on the current iteration, the color of it on the next iteration will be the same.
- Otherwise, the color of the cell on the next iteration will be different.
Two cells are adjacent if they have a mutual edge.
Now Orac has set an initial situation, and he wants to know for the cell $ (i,j) $ (in $ i $ -th row and $ j $ -th column), what will be its color at the iteration $ p $ . He may ask you these questions several times.



思路
我们思考,假设存在一个格子能变色,则它一直能变色,因为它旁边必然存在一个格子与它颜色始终相同。
再思考,假如一个格子能变色,它旁边的一个格子不能变色,那么再第一个格子第一次变色后,第二个格子也能变色。因为第一个格子变色后颜色就与第二个格子相同了。
综上所述,我们可以发现变色存在传染性。
因此,我们可以把初始棋盘中能变色的时间戳记为0,然后用广搜把所有格子开始变色的时间戳都求出来。
这个过程中要注意的是:
- 初始棋盘有可能所有格子都不变色
- 若询问时间小于开始变色时间,应输出初始颜色
总时间复杂度:
代码
1 | // Problem: CF1349C Orac and Game of Life |
总结
这道题解题的关键在于发现格子存在传染性。
发现的过程主要是通过模拟和分类讨论实现的。
同理,这类题可以尝试去模拟样例,分类讨论,求出关键性质。





