You ' ve gotten an nxm sheet of squared paper. Some of its squares is 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 so we can delete from set A to do it not connected.
A set of painted squares is called connected, if for every both squares A and b from this set there is a sequence of square s from the set, beginning in a and ending in B, such that in this sequence any square, except for the last one, shares a C Ommon side with the square, follows next in the sequence. An empty set and a set consisting of exactly one square is connected by definition. Input
The first input line contains the 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 of the character Lin E equals either "#", if the corresponding square is painted (belongs to set A), or equals "." If the corresponding square Is isn't painted (does not belong to set A). It is guaranteed, the set of all painted squares A is connected and isn ' t empty. Output
On the first line print the minimum number of squares this need to is deleted to make set A not connected. If It is impossible, print-1. Example Input
5 4
# # # # # # # # #
#.. #
#.. #
####
Output
2
Input
5 5
#####
#...#
#####
#...#
#####
Output
2
Note
In the first sample can delete any of the squares that does not share a side. After, the set of painted squares is not connected anymore.
The note to the second sample was shown on the figure below. The left there was a picture of the initial set of squares. To the right there are a set with deleted squares. The deleted squares is marked with crosses.
Exercises
This problem seems to have no clue, but in fact, it is not difficult to draw a few of their own graphics can be found, arbitrary graphics, it can be removed at most two blocks will be divided into two parts, so, we can start from a lattice, go through all the lattice, bar the current check out, with Dfs to find out if it can be divided into two parts Until all the lattice traversal, if you save a certain lattice after the figure is divided into two parts, the answer is 1, at the same time, if the beginning of the entire graph of the lattice number is less than 3, then there is no answer, output-1 can.
"AC Code"
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace
Std
int m,n;
Char str[55][55];
BOOL Vis[55][55];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void Dfs (int x,int y) {vis[x][y]=1;
for (int i=0;i<4;i++) {int xx = x+dx[i];
int yy = Y+dy[i]; if (xx>=0&&xx<m&&yy>=0&&yy<n&&str[xx][yy]== ' # ' &&vis[xx][yy]
==0) {DFS (XX,YY);
}}} int solve (int x,int y) {int cnt=0;
memset (vis,0,sizeof Vis);
Vis[x][y]=1;
for (int i=0;i<m;++i) for (int j=0;j<n;++j) if (str[i][j]== ' # ' &&!vis[i][j]) {
DFS (I,J);
cnt++;
} return CNT; } int cal () {for (Int. i=0;i<m;++i) for (int j=0;j<n;++j) if (str[i][j]== ' # ' && Solve (
I,J) >1) return 1;
return 0; } int Main () {WHile (~scanf ("%d%d", &m,&n)) {GetChar ();
int ans=0;
int s=100;
for (int i=0;i<m;++i) {for (int j=0;j<n;++j) {scanf ("%c", &str[i][j]);
if (str[i][j]== ' # ') ans++;
} getchar ();
} if (ans<3) printf (" -1\n");
else if (Cal ()) {printf ("1\n");
} else printf ("2\n");
} return 0; }