本文主要是介绍暑期训练赛(6)E,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.
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.
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.
5 4 #### #..# #..# #..# ####
2
5 5 ##### #...# ##### #...# #####
2
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;
}
这篇关于暑期训练赛(6)E的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!