A16078 | Matrix Cascade
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is a matrix of size $n \times n$ which consists of 0s and 1s. The rows are numbered from $1$ to $n$ from top to bottom, the columns are numbered from $1$ to $n$ from left to right. The cell at the intersection of the $x$ -th row and the $y$ -th column is denoted as $(x, y)$ .
AquaMoon wants to turn all elements of the matrix to 0s. In one step she can perform the following operation:
- Select an arbitrary cell, let it be $(i, j)$ , then invert the element in $(i, j)$ and also invert all elements in cells $(x, y)$ for $x > i$ and $x - i \ge \left|y - j\right|$ . To invert a value means to change it to the opposite: 0 changes to 1, 1 changes to 0.
Help AquaMoon determine the minimum number of steps she need to perform to turn all elements of the matrix to 0s. We can show that an answer always exists.
AquaMoon wants to turn all elements of the matrix to 0s. In one step she can perform the following operation:
- Select an arbitrary cell, let it be $(i, j)$ , then invert the element in $(i, j)$ and also invert all elements in cells $(x, y)$ for $x > i$ and $x - i \ge \left|y - j\right|$ . To invert a value means to change it to the opposite: 0 changes to 1, 1 changes to 0.
Help AquaMoon determine the minimum number of steps she need to perform to turn all elements of the matrix to 0s. We can show that an answer always exists.
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 10^5$ ). The description of the test cases follows.
The first line of each test case contains an integer $n$ ( $2 \le n \le 3000$ ).
The $i$ -th of the following $n$ lines contains a binary string only of characters 0 and 1, of length $n$ .
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $9\,000\,000$ .
The first line of each test case contains an integer $n$ ( $2 \le n \le 3000$ ).
The $i$ -th of the following $n$ lines contains a binary string only of characters 0 and 1, of length $n$ .
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $9\,000\,000$ .
输出格式
For each test case, print the minimum number of steps.
输入输出样例
输入 #1
3 5 00100 01110 11111 11111 11111 3 100 110 110 6 010101 111101 011110 000000 111010 001110
输出 #1
1 2 15
In the first test case, we can use the following scheme:
1. perform the operation on the cell $(1, 3)$ .
Clearly, the elements of the initial matrix are not all 0, so at least one operation is required. Thus, $1$ is the answer.
In the second test case, we use the following scheme:
1. perform the operation on the cell $(3, 3)$ ;
2. perform the operation on the cell $(1, 1)$ .
It can be shown that there is no way to convert all elements to 0s in $0$ or $1$ steps, so the answer is exactly $2$ .
1. perform the operation on the cell $(1, 3)$ .
Clearly, the elements of the initial matrix are not all 0, so at least one operation is required. Thus, $1$ is the answer.
In the second test case, we use the following scheme:
1. perform the operation on the cell $(3, 3)$ ;
2. perform the operation on the cell $(1, 1)$ .
It can be shown that there is no way to convert all elements to 0s in $0$ or $1$ steps, so the answer is exactly $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted