Codeforces 193A. Cutting Figure
看起来非常神,但仅仅有三种情况 -1 , 1 ,2.....
2 seconds
256 megabytes
standard input
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.
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.
/**
* Created by ckboss on 14-10-8.
*/
import java.util.*; public class CuttingFigure {
static int n,m;
static int[] dir_x = {0,0,1,-1};
static int[] dir_y = {1,-1,0,0};
static boolean[][] vis = new boolean[55][55];
static char[][] map = new char[55][55];
static boolean inmap(int x,int y){
return (x>=0&&x<n)&&(y>=0&&y<m);
} static void dfs(int x,int y){
vis[x][y]=true;
for(int i=0;i<4;i++){
int X=dir_x[i]+x;
int Y=dir_y[i]+y;
if(inmap(X,Y)&&vis[X][Y]==false&&map[X][Y]=='#'){
dfs(X,Y);
}
}
} static int CountNum(int x,int y){
for(int i=0;i<55;i++) Arrays.fill(vis[i],false);
if(inmap(x,y)) vis[x][y]=true;
int cnt=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis[i][j]==false&&map[i][j]=='#'){
dfs(i,j);
cnt++;
}
}
}
return cnt;
} public static void main(String[] args){
Scanner in = new Scanner(System.in);
n=in.nextInt(); m=in.nextInt();
int nb=0;
in.nextLine();
for(int i=0;i<n;i++){
String line = in.nextLine();
for(int j=0;j<m;j++){
map[i][j]=line.charAt(j);
if(map[i][j]=='#') nb++;
}
}
if(nb<3){
System.out.println("-1");
return ;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(CountNum(i,j)==1){
continue;
}
else {
System.out.println("1");
return ;
}
}
}
System.out.println("2");
}
}
Codeforces 193A. Cutting Figure的更多相关文章
- Codeforces 1077D Cutting Out(二分答案)
题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...
- CodeForces 998B Cutting(贪心)
https://codeforces.com/problemset/problem/998/B 简单贪心题 代码如下: #include <stdio.h> #include <st ...
- Codeforces 1162D Chladni Figure(枚举因子)
这个题好像可以直接暴力过.我是先用num[len]统计所有每个长度的数量有多少,假如在长度为len下,如果要考虑旋转后和原来图案保持一致,我们用a表示在一个旋转单位中有几个长度为len的线段,b表示有 ...
- @codeforces - 594E@ Cutting the Line
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个字符串 s 与正整数 k.现在你需要进行恰好一次操作: ...
- codeforces 1077D Cutting Out 【二分】
题目:戳这里 题意:给n个数的数组,要求找k个数满足,这k个数在数组中出现的次数最多. 解题思路:k个数每个数出现次数都要最大化,可以想到二分下限,主要是正确的二分不好写. 附ac代码: 1 #inc ...
- Codeforces Round #122 (Div. 2)
A. Exams 枚举分数为3.4.5的数量,然后计算出2的数量即可. B. Square 相当于求\(\min{x(n+1)\ \%\ 4n=0}\) 打表发现,对\(n\ \%\ 4\)分类讨论即 ...
- ACdream区域赛指导赛之手速赛系列(2)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/DaiHaoC83E15/article/details/26187183 回到作案现场 ...
- ACDream手速赛2
地址:http://acdream.info/onecontest/1014 都是来自Codeforce上简单题. A. Boy or Girl 简单字符串处理 B. Walking in ...
- 贪心 Codeforces Round #300 A Cutting Banner
题目传送门 /* 贪心水题:首先,最少的个数为n最大的一位数字mx,因为需要用1累加得到mx, 接下来mx次循环,若是0,输出0:若是1,输出1,s[j]--: 注意:之前的0的要忽略 */ #inc ...
随机推荐
- 我的Android进阶之旅------>Android字符串资源中的单引號问题error: Apostrophe not preceded by 的解决的方法
刚刚在string字符串资源文件里,写了一个单引號.报错了,错误代码例如以下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资 ...
- iOS项目开发实战——使用CoreLocation获取当前位置信息
随着基于位置服务LBS和移动互联网的兴起,你的位置是越来越重要的一个信息.位置服务已经是当前的热门应用如微信.陌陌等社交应用的杀手锏.而在iOS开发中,苹果已经给我们提供了一个位置接口.CoreLoc ...
- draw call 理解和优化
draw call是openGL的描绘次数(directX没怎么研究,但原理应该差不多)一个简单的openGL的绘图次序是:设置颜色→绘图方式→顶点座标→绘制→结束.每帧都会重复以上的步骤.这就是一次 ...
- Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享
Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享 2014-09-09 14:14:25 标签:会话共享 主从 原创作品,允许转载,转载时请务必以超链接形式标明文章 ...
- HTML5&CSS3初学者指南
介绍 网络时代已经到来.现在对人们来说,每天上网冲浪已经成为一种最为常见的行为. 一个典型的网页是由文本.图像和链接组成的.除去内容上的差异,不同网站的网页也具有不同的外观和感受,以实现在网络上建立自 ...
- mongodb - Master Slave Replication
master-slave复制模式大多场景下都被replicat sets代替.官方也建议使用replicat sets. master-slave复制不支持自动failover. master-sla ...
- android 中的 Handler 线程间通信
一. 在MainActivity中为什么只是类似的写一行如下代码就可以使用handler了呢? Handler handler = new Handler() { @Override public v ...
- Android工程:引用另一个Android工程的方法详解
本篇文章是对在Android中引用另一个Android工程的方法进行了详细的分析介绍.需要的朋友参考下 现在已经有了一个Android工程A.我们想扩展A的功能,但是不想在A的基础上做开发,于是新 ...
- FreeMarker静态化文件解决SEO推广问题
1.问题背景 SEO一直是站点对外推广的一个重要手段,如何可以让搜索引擎高速搜索到站点对于增强站点的浏量,提升站点对外形象有着重要意义.那么如何可以对SEO进行优化呢?一个很经常使用的手段就是在网页的 ...
- 快速理解linux流编辑器sed命令
原创 杜亦舒性能与架构 之前介绍过 awk 命令,sed 命令同样是非常重要的文本处理工具,涉及到linux shell开发时,几乎是避不开这两大利器的 sed 是 stream editor 的简写 ...