暑期训练赛(6)E

来源:互联网 发布:知乎 朴宝剑 编辑:程序博客网 时间:2024/04/28 22:19
C. Cutting Figure
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You've gotten an n × m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected.

A set of painted squares is called connected, if for every two squares a and b from this set there is a sequence of squares from the set, beginning in a and ending in b, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition.

Input

The first input line contains two space-separated integers n and m (1 ≤ n, m ≤ 50) — the sizes of the sheet of paper.

Each of the next n lines contains m characters — the description of the sheet of paper: the j-th character of the i-th line equals either "#", if the corresponding square is painted (belongs to set A), or equals "." if the corresponding square is not painted (does not belong to set A). It is guaranteed that the set of all painted squares A is connected and isn't empty.

Output

On the first line print the minimum number of squares that need to be deleted to make set A not connected. If it is impossible, print -1.

Sample test(s)
input
5 4#####..##..##..#####
output
2
input
5 5######...#######...######
output
2
Note

In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.

The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses.

//AC代码

/*从此题可以推出答案只有三种:-1,1,2首先处理掉-1的情况,也就是初始只有0,1,2个#的情况然后只需要枚举#,然后进行搜索dfs,判断是否能够做到所有的点就OK了否则就是2*/#include<iostream>#include<queue>#include<cstdio>#include<algorithm>#include<cstring>#include<iomanip>#include<map>#include<cstdlib>#include<cmath>#include<vector>#define LL long long#define IT __int64#define zero(x) fabs(x)<eps#define mm(a,b) memset(a,b,sizeof(a))const int INF=0x7fffffff;const double inf=1e8;const double eps=1e-10;const double PI=acos(-1.0);const int MAXN=60;using namespace std;int n, m, ps, move[4][2]= {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};bool isPaint[MAXN][MAXN], isVis[MAXN][MAXN];void input(){    cin>>n>>m;    for (int i=0; i<n; ++i)        for (int j=0; j<m; ++j)        {            char c;            cin>>c;            if (c=='#')            {                isPaint[i][j]=true;                ps++;            }        }}bool isValid(int x, int y){    return 0<=x&&x<n&&0<=y&&y<m;}void dfs(int x, int y){    isVis[x][y]=true;    for (int i=0; i<4; ++i)    {        int p=x+move[i][0], q=y+move[i][1];        if (isValid(p, q) && isPaint[p][q] && !isVis[p][q])            dfs(p, q);    }}bool isConnect(){    int x, y;    for (int i=0; i<n; ++i)        for (int j=0; j<m; ++j)            if (isPaint[i][j])            {                x=i;                y=j;            }    memset(isVis, false, sizeof(isVis));    dfs(x, y);    for (int i=0; i<n; ++i)        for (int j=0; j<m; ++j)            if (isPaint[i][j] && !isVis[i][j])                return false;    return true;}int main(){    input();    if (ps==1 || ps==2)    {        cout<<-1<<endl;        return 0;    }    for (int i=0; i<n; ++i)        for (int j=0; j<m; ++j)            if (isPaint[i][j])            {                isPaint[i][j]=false;                if (!isConnect())                {                    cout<<1<<endl;                    return 0;                }                isPaint[i][j]=true;            }    cout<<2<<endl;    return 0;}


 

0 0
原创粉丝点击