A16274 | Constructive Problems
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Gridlandia has been hit by flooding and now has to reconstruct all of it's cities. Gridlandia can be described by an $n \times m$ matrix.
Initially, all of its cities are in economic collapse. The government can choose to rebuild certain cities. Additionally, any collapsed city which has at least one vertically neighboring rebuilt city and at least one horizontally neighboring rebuilt city can ask for aid from them and become rebuilt without help from the government. More formally, collapsed city positioned in $(i, j)$ can become rebuilt if both of the following conditions are satisfied:
- At least one of cities with positions $(i + 1, j)$ and $(i - 1, j)$ is rebuilt;
- At least one of cities with positions $(i, j + 1)$ and $(i, j - 1)$ is rebuilt.
If the city is located on the border of the matrix and has only one horizontally or vertically neighbouring city, then we consider only that city.
 Illustration of two possible ways cities can be rebuilt by adjacent aid. White cells are collapsed cities, yellow cells are initially rebuilt cities (either by the government or adjacent aid), and orange cells are rebuilt cities after adjacent aid.The government wants to know the minimum number of cities it has to rebuild such that after some time all the cities can be rebuild.
Initially, all of its cities are in economic collapse. The government can choose to rebuild certain cities. Additionally, any collapsed city which has at least one vertically neighboring rebuilt city and at least one horizontally neighboring rebuilt city can ask for aid from them and become rebuilt without help from the government. More formally, collapsed city positioned in $(i, j)$ can become rebuilt if both of the following conditions are satisfied:
- At least one of cities with positions $(i + 1, j)$ and $(i - 1, j)$ is rebuilt;
- At least one of cities with positions $(i, j + 1)$ and $(i, j - 1)$ is rebuilt.
If the city is located on the border of the matrix and has only one horizontally or vertically neighbouring city, then we consider only that city.
 Illustration of two possible ways cities can be rebuilt by adjacent aid. White cells are collapsed cities, yellow cells are initially rebuilt cities (either by the government or adjacent aid), and orange cells are rebuilt cities after adjacent aid.The government wants to know the minimum number of cities it has to rebuild such that after some time all the cities can be rebuild.
输入格式
Each test consists of multiple test cases. The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases. The description of the test cases follows.
The only line of each test case contains two integers $n$ and $m$ ( $2 \le n, m \le 100$ ) — the sizes of Gridlandia.
The only line of each test case contains two integers $n$ and $m$ ( $2 \le n, m \le 100$ ) — the sizes of Gridlandia.
输出格式
For each test case, output a single integer — the minimum number of cities the government needs to rebuild.
输入输出样例
输入 #1
3 2 2 5 7 3 2
输出 #1
2 7 3
In the first test case, it's enough for the government to rebuild cities $(1, 2)$ and $(2, 1)$ .
In the second test case, it's enough for the government to rebuild cities $(1, 4)$ , $(2, 2)$ , $(3, 1)$ , $(3, 6)$ , $(4, 3)$ , $(5, 5)$ , $(5, 7)$ .
In the second test case, it's enough for the government to rebuild cities $(1, 4)$ , $(2, 2)$ , $(3, 1)$ , $(3, 6)$ , $(4, 3)$ , $(5, 5)$ , $(5, 7)$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted