#abc283e. E - Don't Isolate Elements
E - Don't Isolate Elements
Score : points
问题描述
你得到一个矩阵 ,它有 行和 列。其每个元素的值为 或 。对于满足条件 和 的整数对 ,我们用 表示第 行第 列的元素。
你可以对矩阵 执行以下操作任意多次(可能为零次):
- 选择一个满足 的整数 。对于每一个满足 的整数 ,将 的值替换为 。
若 满足以下条件,则称其为 孤立元素:不存在与其相邻且具有相同值的元素;换句话说,如果不存在四个整数对 满足 以及 。
确定是否可以通过重复执行上述操作,使得矩阵 处于没有孤立元素的状态。如果可能,请找到完成此目标所需的最少操作次数。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given a matrix with rows and columns. The value of each of its elements is or . For an integer pair such that and , we denote by the element at the -th row and -th column.
You can perform the following operation on the matrix any number of times (possibly zero):
- Choose an integer such that . For every integer such that , replace the value of with .
is said to be isolated if and only if there is no adjacent element with the same value; in other words, if and only if none of the four integer pairs satisfies , and .
Determine if you can make the matrix in such a state that no element is isolated by repeating the operation. If it is possible, find the minimum number of operations required to do so.
Constraints
- or
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
If you can make it in such a state that no element is isolated by repeating the operation, print the minimum number of operations required to do so; otherwise, print -1
.
Sample Input 1
3 3
1 1 0
1 0 1
1 0 0
Sample Output 1
1
An operation with makes , where there is no longer an isolated element.
Sample Input 2
4 4
1 0 0 0
0 1 1 1
0 0 1 0
1 1 0 1
Sample Output 2
2
Sample Input 3
2 3
0 1 0
0 1 1
Sample Output 3
-1
update @ 2024/3/10 11:52:37