Codeforces 193A. Cutting Figure


看起来很神,但只有三种情况 -1 , 1 ,2.....

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





/**
 * 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");
    }
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值