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×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.

There is at least one south magnet in every row and every column.
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.
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).

Input
The first line contains two integers n and m (1≤n,m≤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
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.

Examples
Input
3 3
.#.

##.
Output
1
Input
4 2

.#
.#

Output
-1
Input
4 5
…#
####.
.###.
.#…
Output
2
Input
2 1
.

Output
-1
Input
3 5



Output
0
Note
In the first test, here is an example placement of magnets:

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.

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.

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.

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.
题意:#代表黑色,.代表白色。N磁铁会被S磁铁吸引,每秒走一个格子,去往S所在的位置。
有以下几个要求:
1.任意一行和一列都要有至少一个S磁铁,但是不限制个数。
2.N磁铁不能在白色上面。
3.N磁铁的个数要尽量的少。
4.N磁铁要遍所有的黑色格子。
问N磁铁最少的个数是多少?
思路:题意读明白之后,其实S磁铁不限制个数,那么我们可以把所有的格子在符合条件的前提下都放置上S磁铁。那么 这个题目就转换成了求连通块的个数,连通块的个数就是N磁铁最少的数目。但是要特判-1的情况。
①跟样例二类似:如果一行或者一列的黑色格子中间夹杂着白色格子,那么是不可以的。
②跟样例四类似:如果有一行或者一列都是白色格子,但是对应的一列或一行都有黑色格子,这也是不可以的。
如图所示:

第三四列都是白色的格子,但是第一二行都有黑色的格子,这样是不可以的。不能满足所有的条件。

行都是白色格子的时候亦如此。
其余的情况都是符合条件的了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e3+100;
char s[maxx][maxx];
int vis[maxx][maxx];
int d[][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m,mk1=0,mk2=0;inline void dfs(int x,int y)
{vis[x][y]=1;for(int i=0;i<4;i++){int tx=x+d[i][0];int ty=y+d[i][1];if(tx<0||tx>=n||ty<0||ty>=m||vis[tx][ty]||s[tx][ty]=='.') continue;dfs(tx,ty);}
}
inline int Judge1()
{int flag=1;for(int i=0;i<n;i++){flag=1;for(int j=0;j<m;){if(s[i][j]=='#'&&flag) {while(j<m&&s[i][j]=='#') j++;flag=0;}else if(s[i][j]=='#'&&flag==0) return 1;else j++;}if(flag==1&&mk2) return 1;}return 0;
}
inline int Judge2()
{int flag=1;for(int j=0;j<m;j++){flag=1;for(int i=0;i<n;){if(s[i][j]=='#'&&flag){while(i<n&&s[i][j]=='#') i++;flag=0;}else if(s[i][j]=='#'&&!flag) return 1;else i++;}if(flag&&mk1) return 1;}return 0;
}
int main()
{scanf("%d%d",&n,&m);for(int i=0;i<n;i++) scanf("%s",s[i]);for(int i=0;i<n;i++){mk1=0;for(int j=0;j<m;j++) {if(s[i][j]=='#') mk1=1;}if(mk1==0) break;}for(int j=0;j<m;j++){mk2=0;for(int i=0;i<n;i++){if(s[i][j]=='#') mk2=1;}if(mk2==0) break;}if(Judge1()||Judge2()) cout<<"-1"<<endl;else{memset(vis,0,sizeof(vis));int ans=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(s[i][j]=='#'&&vis[i][j]==0){ans++;dfs(i,j);}}}cout<<ans<<endl;}return 0;
}

努力加油a啊,(o)/~

Monopole Magnets CodeForces - 1345D(dfs+思维)相关推荐

  1. CodeForces - 1344D Monopole Magnets(dfs)

    题目链接:点击查看 题目大意:给出一个 n * m 的矩阵,' # ' 代表黑色方格,' . ' 代表白色方格,现在可以在任意方格上摆放任意个单向磁铁,磁铁具有的一个性质是:每秒钟 N 极磁铁会向同行 ...

  2. Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) dfs + 思维

    传送门 文章目录 题意: 思路: 题意: 给一张图,求必须经过aaa点和bbb点的路径条数. 思路: 通过观察我们发现,这个路径无非就是x−>a−>b−>yx->a->b ...

  3. Codeforces Round #607 (Div. 2) E. Jeremy Bearimy dfs + 思维

    传送门 文章目录 题意: 思路: 题意: 给你2∗k2*k2∗k个点的一棵树.定义GGG为任选kkk组不同的点,每组点的距离和的最小值.定义BBB为任选kkk组不同的点,每组点的距离和的最大值.让你求 ...

  4. codeforces 有意思的思维题 1 ~ 15

    codeforces 思维题 1.给定数组,求满足i < j and ai * aj = i + j的数对数量 2.第 i 步向前跳 i 步或后退 1 步 3.给两个点,求正方形的另两个点 4. ...

  5. Codeforces 19E DFS 树

    题意 传送门 Codeforces 19E Fairy 题解 若图中不存在非二分图的连通分量,则任意一边删除后仍是二分图:若图中存在大于一个非二分图的连通分量,则不可能通过删除一条边使之变为二分图.故 ...

  6. Codeforces 962F DFS 树

    题意 传送门 Codeforces 962F Simple Cycles Edges 题解 任一个简单环,都可以通过取 DFSDFSDFS 树单条非树边与树边构成的环的集合 SSS 的任意子集异或得到 ...

  7. codeforces数学1600day4[贪心数学公式推导CodeForces - 1151D ,思维CodeForces - 1085C,数论同余+组合计数 CodeForces - 1056B]

    A - Stas and the Queue at the Buffet CodeForces - 1151D 题目大意:就是给你n个人在排队,每个人都有一个ai值和bi值,每个人的不满意度就是f(i ...

  8. CF982 C Cut 'em all!【树/DFS/思维】

    [链接]:CF982C [题意]:有一颗树,你需要切掉一些边,使这颗树分拆成若干个节点为偶数的联通分量,最多能切掉几条边.若不能切,输出-1. [分析]: 1.若点数n为奇数,因为奇数不可能分为偶数, ...

  9. Divide by three, multiply by two CodeForces - 977D (思维排序)

    Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, a ...

最新文章

  1. python怎么样处理excel教程_python处理excel教程是什么
  2. 程序语言python循环_《python语言程序设计》_第5章_循环
  3. 百度ERNIE新突破,登顶中文医疗信息处理权威榜单CBLUE冠军
  4. php class variable,解决关于PHP“Undefined variable”变量未定义
  5. 20-100-010-安装-Flink集群安装 flink-1.4.0-bin-hadoop27-scala_2.11
  6. Linux snmp 时间戳,linux snmp计数器汇总
  7. ruby入门tips
  8. Java 匹配域名正则表达式
  9. JSP入门之表格以及常用表单元素(总结自身编程经验以及多本教科书)
  10. php内存占用越来越高,代码编辑器 - phpstrom启动非常慢,完了占用内存很多,有没有什么方法解决...
  11. 利用VS2019对程序进行时间性能分析
  12. Mac升级系统后,Android Studio 不能用问题
  13. 手机尾号猜年龄骗局解密
  14. 拼多多无货源开店需要用哪些店群软件
  15. shiro基本使用和完成登录
  16. 老大让我看baidu他们的查公交是怎么做的,我就看了
  17. 数值分析——LU分解求解线性方程组的Python实现
  18. 专杀工具编写思路(转)
  19. 面试题--maven和tomcat篇
  20. 工作总结-理财需求分析

热门文章

  1. Android—View事件分发与View子类
  2. IOS基础之仿酷狗音乐第1天
  3. 南方s730手簿说明书_最新S730手簿及3.0简易操作82
  4. kali Linux/centos7 Linux设置不会屏保
  5. 开源 多进程 框架 c++_linux fork多进程并发服务器模型之C/C++代码实战
  6. poco c++ 开发指南_掌握这个框架,你将会开发通杀全平台的万能爬虫
  7. linux红帽子怎么配置dhcp,LinuxDHCP的高级配置如何应用呢?
  8. Android开发之高德地图定位成功返回的定位信息
  9. Linux实战教学笔记52:GlusterFS分布式存储系统
  10. 杭州人有福了!菜鸟配送升级新增24小时送药服务