暑期训练赛(6)E
来源:互联网 发布:知乎 朴宝剑 编辑:程序博客网 时间:2024/04/28 22:19
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
- hpu暑期训练:E
- 暑期训练赛(6)解题报告
- 暑期训练赛(6)A题
- 暑期训练赛(6)B
- 暑期训练赛(6)C
- 暑期训练赛(6)D
- 2013 - ECJTU 暑期训练赛第二场-problem-E
- 2013 - ECJTU 暑期训练赛第三场-problem-E
- 2013 - ECJTU 暑期训练赛第八场-problem-E
- 浙江中医药大学暑期训练测试赛八E
- CSU-ACM2017暑期训练4-dfs E
- CSU-ACM2017暑期训练5-三分 E
- CSU-ACM2017暑期训练5-三分 E
- CSU-ACM2017暑期训练12-KMP E
- 2013 - ECJTU 暑期12级训练赛第一场-problem-E
- 暑期二(E)(hdu2073)
- CSU-ACM2017暑期训练1-Debug与STL E
- POJ 2418——Hardwood Species c++STL(map的应用)
- 【Android】细说visibility属性
- 获取Android状态栏高度的屡试不爽的方法
- JAVA双缓存机制
- Openfire插件开发流程
- 暑期训练赛(6)E
- Hibernate学习笔记二
- C++输入N个字符串,将他们依次保存到一个字符串数组中,怎么表示字符串输入完成?
- shell学习
- SQLite FAQ常见问题解答
- 2014-8-5-笔记
- freemarker 学习笔记
- sqlite的字段类型
- HDU 4915 Parenthese sequence