Codeforces-194C-Cutting Figure (思维+bfs)

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.

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


题意:给一个n*m的图,#组成一个连通块,问最少去掉几个#使连通块分开成两个,只有上下左右相邻算相连;

思路:分析一下可以发现,不管是一个怎样的连通块,最多移掉2个就可以把连通块分开;数据比较小,直接枚举去掉一个和去掉两个的所有情况。当#的个数小于3的时候输出-1;

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
char str[55][55];
int n, m, cnt;
bool vis[55][55];
struct node
{
	int x, y;
}tmp,p,arr[2505];
queue<node> que;
int dir[4][2] = { 1,0,-1,0,0,1,0,-1 };
bool check(int x, int y)
{
	if (x<1 || x>n || y<1 || y>m || str[x][y] != '#' || vis[x][y]) return false;
	return true;
}
int bfs()
{
	int ans = 0;
	while (que.size())
	{
		ans++;
		tmp = que.front();
		que.pop();
		for (int i = 0; i < 4; i++)
		{
			int tx = tmp.x + dir[i][0];
			int ty = tmp.y + dir[i][1];
			if (check(tx, ty))
			{
				p.x = tx;
				p.y = ty;
				vis[tx][ty] = true;
				que.push(p);
			}
		}
	}
	return ans;
}
int check(int x, int y, int a, int b,int tag)
{
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (str[i][j] == '#'&&i != x&&j != y) {
				vis[i][j] = vis[x][y] = vis[a][b] = true;
				p.x = i; p.y = j;
				que.push(p);
				if (bfs() == tag) return 0;
				else  return 1;
			}
		}
	}
}
int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		for (int i = 1; i <=n; i++)
			scanf("%s", str[i] + 1);
		cnt = 0;
		int c = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				if (str[i][j] == '#') {
					arr[++c].x = i;
					arr[c].y = j;
				}
			}
		}
		if (c < 3) {
		      printf("-1\n");
		      continue;
		}
		int flag = 0;
		for (int i = 1; i <= c; i++) {//移掉一个#
                       if (check(arr[i].x, arr[i].y, 0, 0,c-1)) {
                            printf("1\n");
                            flag = 1; break;
                       }
		}
		if (flag) continue;
		for (int i = 1; i <= c; i++) {//移掉2个#
			for (int j = i + 1; j <= c; j++) {
				if (check(arr[i].x, arr[i].y, arr[i].x, arr[j].y,c-2)) {
					flag = 1;
					printf("2\n");
					break;
				}
			}
			if (flag) break;
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_37171272/article/details/78969790