307352: CF1344B. Monopole Magnets

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Monopole Magnets

题意翻译

### 题目描述 **单极磁铁**,顾名思义,就是只有一个磁极(N 或 S)的磁铁,~~他们在实际生活中是不存在的,不过……不要在意那些细节。~~ 我们有一个 $n$ 行 $m$ 列的方格图,要在里面放一些单极磁铁,你可以随便放置,甚至把多个磁铁放在同一个格子里。 单极磁铁的**吸引**是这么定义的:任选一个 N 极磁铁和一个 S 极磁铁,如果它们两个**恰好处于同一行或同一列中**,那 N 极磁铁会向**靠近 S 极磁铁**的方向**前进一格**。当然,如果它们两个处于同一个格子,什么也不会发生。注意 **S 极磁铁的位置是永远不会变化的**。 这个方格图里的每一个格子都涂成了**黑色**或**白色**。磁铁的放置必须要**遵守以下规则**: 1. 每行每列都必须至少有一个 S 极磁铁。 2. 对于每个黑色格子,必须有某个 N 极磁铁通过一系列的吸引操作经过它。 3. 对于每个白色格子,无论 N 极磁铁的位置怎样变化,都不能经过它。 现在我们想知道:要满足以上三条规则,至少要放几个 N 极磁铁。如果无论怎么放都不能满足规则,请输出 $-1$。 ### 输入格式 第一行有两个整数 $n$ 和 $m\;(1\le n,m\le 1000)$ ,表示方格图的行数和列数。 接下来 $n$ 行,每行 $m$ 个字符,是对整个方格图进行描述。如果第 $i$ 行的第 $j$ 个字符为 `#`,表示这个格子是黑色的,如果为 `.`,表示这个格子是白色的。 保证除了 `#` 和 `.` 外不会出现其它奇奇怪怪的字符。 ### 输出格式 输出一个整数,表示要满足规则,要放置的 N 极磁铁的最少个数,如果无解,请输出 $-1$。 (translated by 珂爱的 yangrunze)

题目描述

A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so we don't care. There is an $ n\times m $ grid. Initially, you may place some north magnets and some south magnets into the cells. You are allowed to place as many magnets as you like, even multiple in the same cell. An operation is performed as follows. Choose a north magnet and a south magnet to activate. If they are in the same row or the same column and they occupy different cells, then the north magnet moves one unit closer to the south magnet. Otherwise, if they occupy the same cell or do not share a row or column, then nothing changes. Note that the south magnets are immovable. Each cell of the grid is colored black or white. Let's consider ways to place magnets in the cells so that the following conditions are met. 1. There is at least one south magnet in every row and every column. 2. If a cell is colored black, then it is possible for a north magnet to occupy this cell after some sequence of operations from the initial placement. 3. If a cell is colored white, then it is impossible for a north magnet to occupy this cell after some sequence of operations from the initial placement. Determine if it is possible to place magnets such that these conditions are met. If it is possible, find the minimum number of north magnets required (there are no requirements on the number of south magnets).

输入输出格式

输入格式


The first line contains two integers $ n $ and $ m $ ( $ 1\le n,m\le 1000 $ ) — the number of rows and the number of columns, respectively. The next $ n $ lines describe the coloring. The $ i $ -th of these lines contains a string of length $ m $ , where the $ j $ -th character denotes the color of the cell in row $ i $ and column $ j $ . The characters "\#" and "." represent black and white, respectively. It is guaranteed, that the string will not contain any other characters.

输出格式


Output a single integer, the minimum possible number of north magnets required. If there is no placement of magnets that satisfies all conditions, print a single integer $ -1 $ .

输入输出样例

输入样例 #1

3 3
.#.
###
##.

输出样例 #1

1

输入样例 #2

4 2
##
.#
.#
##

输出样例 #2

-1

输入样例 #3

4 5
....#
####.
.###.
.#...

输出样例 #3

2

输入样例 #4

2 1
.
#

输出样例 #4

-1

输入样例 #5

3 5
.....
.....
.....

输出样例 #5

0

说明

In the first test, here is an example placement of magnets: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/64081a77e42ae7a50ec16156938f7edaddb418dd.png)In the second test, we can show that no required placement of magnets exists. Here are three example placements that fail to meet the requirements. The first example violates rule $ 3 $ since we can move the north magnet down onto a white square. The second example violates rule $ 2 $ since we cannot move the north magnet to the bottom-left black square by any sequence of operations. The third example violates rule $ 1 $ since there is no south magnet in the first column. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/74342bffb2ed25274f81d33fb996a7cec774a68f.png)In the third test, here is an example placement of magnets. We can show that there is no required placement of magnets with fewer north magnets. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/0515bcb41bc9913bf5f95900134aa4f65b7e2af6.png)In the fourth test, we can show that no required placement of magnets exists. Here are two example placements that fail to meet the requirements. The first example violates rule $ 1 $ since there is no south magnet in the first row. The second example violates rules $ 1 $ and $ 3 $ since there is no south magnet in the second row and we can move the north magnet up one unit onto a white square. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1344B/72d166e7967b6aa45c82ffda8c7e527d7a3ecbc3.png)In the fifth test, we can put the south magnet in each cell and no north magnets. Because there are no black cells, it will be a correct placement.

Input

题意翻译

### 题目描述 **单极磁铁**,顾名思义,就是只有一个磁极(N 或 S)的磁铁,~~他们在实际生活中是不存在的,不过……不要在意那些细节。~~ 我们有一个 $n$ 行 $m$ 列的方格图,要在里面放一些单极磁铁,你可以随便放置,甚至把多个磁铁放在同一个格子里。 单极磁铁的**吸引**是这么定义的:任选一个 N 极磁铁和一个 S 极磁铁,如果它们两个**恰好处于同一行或同一列中**,那 N 极磁铁会向**靠近 S 极磁铁**的方向**前进一格**。当然,如果它们两个处于同一个格子,什么也不会发生。注意 **S 极磁铁的位置是永远不会变化的**。 这个方格图里的每一个格子都涂成了**黑色**或**白色**。磁铁的放置必须要**遵守以下规则**: 1. 每行每列都必须至少有一个 S 极磁铁。 2. 对于每个黑色格子,必须有某个 N 极磁铁通过一系列的吸引操作经过它。 3. 对于每个白色格子,无论 N 极磁铁的位置怎样变化,都不能经过它。 现在我们想知道:要满足以上三条规则,至少要放几个 N 极磁铁。如果无论怎么放都不能满足规则,请输出 $-1$。 ### 输入格式 第一行有两个整数 $n$ 和 $m\;(1\le n,m\le 1000)$ ,表示方格图的行数和列数。 接下来 $n$ 行,每行 $m$ 个字符,是对整个方格图进行描述。如果第 $i$ 行的第 $j$ 个字符为 `#`,表示这个格子是黑色的,如果为 `.`,表示这个格子是白色的。 保证除了 `#` 和 `.` 外不会出现其它奇奇怪怪的字符。 ### 输出格式 输出一个整数,表示要满足规则,要放置的 N 极磁铁的最少个数,如果无解,请输出 $-1$。 (translated by 珂爱的 yangrunze)

加入题单

上一题 下一题 算法标签: