暑期训练赛(6)E
2014-08-05 18:34
211 查看
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
output
input
output
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代码
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;
}
相关文章推荐
- 2013 - ECJTU 暑期训练赛第三场-problem-E
- 2013 - ECJTU 暑期训练赛第五场-problem-F
- 2013 - ECJTU 暑期训练赛第七场-problem-G
- 中南大学2012暑期集训中期检测训练赛“跳跳”解题报告
- 2013 - ECJTU 暑期训练赛第三场-problem-D
- 2013 - ECJTU 暑期训练赛第三场-problem-L
- 2013 - ECJTU 暑期12级训练赛第一场-Problem-H
- 2013 - ECJTU 暑期训练赛第三场-problem-H
- 暑期训练赛(6)A题
- 2013 - ECJTU 暑期训练赛第四场-problem-A
- 暑期训练赛(6)B
- 2013 - ECJTU 暑期训练赛第四场-problem-C
- 2013 - ECJTU 暑期训练赛第八场-problem-A
- 暑期训练赛(6)C
- 暑期集训Day2训练赛D题Spreadsheets(codeforces 1B)
- 2013 - ECJTU 暑期训练赛第四场-problem-L
- 2013 - ECJTU 暑期训练赛第八场-problem-B
- 暑期训练赛(6)D
- 暑期集训训练赛2(uva11210) 你会打麻将吗?
- 2017暑期ACM俱乐部个人训练赛第2场 G题 Balanced Photo(树状数组求逆序对)