Monopole Magnets CodeForces - 1345D(dfs+思维)

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的情况。
①跟样例二类似:如果一行或者一列的黑色格子中间夹杂着白色格子,那么是不可以的。
②跟样例四类似:如果有一行或者一列都是白色格子,但是对应的一列或一行都有黑色格子,这也是不可以的。
如图所示:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200507193743296.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3N0YXJsZXRfa2lzcw==,size_16,color_FFFFFF,t_7
第三四列都是白色的格子,但是第一二行都有黑色的格子,这样是不可以的。不能满足所有的条件。
在这里插入图片描述
行都是白色格子的时候亦如此。
其余的情况都是符合条件的了。
代码如下:

#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)/~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值