Codeforces Round #639 (Div. 2) D Monopole Magnets

整理的算法模板:ACM算法模板总结(分类详细版)

 

D. Monopole Magnets

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Input

The first line contains two integers nn and mm (1≤n,m≤10001≤n,m≤1000)  — the number of rows and the number of columns, respectively.

The next nn lines describe the coloring. The ii-th of these lines contains a string of length mm, where the jj-th character denotes the color of the cell in row ii and column jj. 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−1.

Examples

input

Copy

3 3
.#.
###
##.

output

Copy

1

input

Copy

4 2
##
.#
.#
##

output

Copy

-1

input

Copy

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

output

Copy

2

input

Copy

2 1
.
#

output

Copy

-1

input

Copy

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

output

Copy

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 33 since we can move the north magnet down onto a white square. The second example violates rule 22 since we cannot move the north magnet to the bottom-left black square by any sequence of operations. The third example violates rule 11 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 11 since there is no south magnet in the first row. The second example violates rules 11 and 33 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.

 

 题意:

 

给你一幅图,‘#’代表黑格,‘.’代表白格,让你在满足以下条件的情况下计算出最少的需要摆放的指北针数,若无解则输出-1

1:每行每列必须至少有一枚指南针

2:指北针能到达所有的黑格

3:指北针不能到达任何一个白格 

如果指北针所在的行和列存在指南针,指北针可以向指南针的方向移动;

 思路:

首先我们考虑无解的情况,通过观察我们可以发现如果每行或每列中的任意两个黑格之间存在白格,那么是无解的,

原因如下:

若两个黑格所在的块中都有一枚指南针,那么当指北针在其中一块时,肯定会被另一块的

指南针所吸引,从而到达白格,违背了条件3

若黑格中全都摆放指北针,如果不违背条件1,那么必须在白格摆放指南针,那么指北针还是能到达白格,违背条件3

若在一个黑块中全都摆放指北针,另一个黑块中摆放指南针,同样会违背条件3

第二种无解的情况:

有全为白格的行(列)但无全为白格的列(行)

这种情况下无论在空行白格处任何地方放指南针都会吸引黑色格子里的指北针到达白格

判断好这两种情况后,只需要dfs求联通块个数即可。

(摘自:https://www.cnblogs.com/ljxdtc666/p/12845431.html讲的真心好)

#include <bits/stdc++.h>
using namespace std;
char g[1005][1005];
int n,m,ans;
int dix[4]={0,0,1,-1},diy[4]={1,-1,0,0};
bool st[1005][1005];
void dfs(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||st[x][y]||g[x][y]=='.') return ;
    st[x][y]=true;
    for(int i=0;i<4;i++)
    {
        int xx=dix[i]+x,yy=diy[i]+y;
        dfs(xx,yy);
    }
}
int main()
{
    cin >>n>>m;
    for(int i=0;i<n;i++)
    {
        getchar();
        for(int j=0;j<m;j++)
        {
            scanf("%c",&g[i][j]);
        }
    }
    int flag1=0,flag2=0,flag3=0;
    for(int i=0;i<n;i++)
    {
        int pre=-1;
        int pos=1;
        for(int j=0;j<m;j++)
        {
            if(g[i][j]=='#')
            {
                if(!st[i][j]) dfs(i,j),ans++;
                
                pos=0;
                if(pre!=-1&&j-pre>1)
                {
                    flag1=1;
                    break;
                }
                pre=j;
            }
        }
        if(pos) flag2=1;
        if(flag1) break;
    }
    for(int j=0;j<m;j++)
    {
        int pre=-1;
        int pos=1;
        for(int i=0;i<n;i++)
        {
            if(g[i][j]=='#')
            {
                pos=0;
                if(pre!=-1&&i-pre>1)
                {
                    flag1=1;
                    break;
                }
                pre=i;
            }
        }
        if(pos) flag3=1;
        if(flag1) break;
    }
    if(flag1||(flag2&&!flag3)||(!flag2&&flag3)) cout <<-1<<endl;
    else cout <<ans<<endl;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值