HUST_ACdream区域赛指导赛之手速赛系列(1)(2)G——BFS——Cutting Figure

Description

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 Input

5 4
####
#..#
#..#
#..#
####

Sample Output

2

Hint

In the sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.

大意:给你一个n*m的图,#表示存在的点,.表示无,#被保证全都连在一起,求最少需要把多少个#转换成.使得它分成两个部分,那么只要遍历图,把当前遍历的#值变成.再对这个.旁边的#做BFS,把联通的都标记成1,最后只要判断有为#但是不为1的点,如果存在说明输出1,否则就2,-1情况check一下~~~坑啊,把BFS里面的一个参数变成了m结果调了一小时~~~微醉,设参需谨慎orz血的教训

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int MAX = 55;
char map1[MAX][MAX];
int flag[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
queue<int>q;
int bfs(){
    int temp1 = 0;
    for(int i = 1; i <= n ; i++)
        for(int j = 1; j <= m ;j++)
            if(map1[i][j] == ‘#‘)
                temp1++;
  if(temp1 <= 2)
 return -1;
    for(int i = 1; i <= n;i++){
        for(int j = 1; j <= m ;j++){
            if(map1[i][j] == ‘#‘){
                memset(flag,0,sizeof(flag));
                int sx = i;
                int sy = j;
                map1[sx][sy] = ‘.‘;
                int dx,dy;
                for(int w = 0 ; w < 4; w++){
                     dx = sx + dir[w][0];
                     dy = sy + dir[w][1];
                   if ( dx >= 1&&dx <= n && dy >= 1 && dy <= m)
                        break;
                }
                while(!q.empty())   q.pop();
                    q.push(dx);
                    q.push(dy);
                    memset(vis,0,sizeof(vis));
                    while(!q.empty()){
                        int x = q.front();
                        q.pop();
                        int y = q.front();
                        q.pop();
                        flag[x][y] = 1;
                        vis[x][y] = 1;
                        for(int w = 0; w < 4; w++){
                            int fx = x + dir[w][0];
                            int fy = y + dir[w][1];
                             if(fx >= 1 && fx <= n && fy >= 1 && fy <= m &&map1[fx][fy] == ‘#‘ && vis[fx][fy] == 0){
                                 q.push(fx);
                                 q.push(fy);
                             }
                        }
                    }
                    int k,l;
                    for( k = 1; k <= n; k++){
                        for( l = 1; l <= m ; l++){
                            if(map1[k][l] == ‘#‘ && flag[k][l] == 0)
                            {
                                return 1;
                            }
                        }
                    }
                    map1[i][j] = ‘#‘;
            }
        }
    }
                return 2;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
    getchar();
       memset(flag,0,sizeof(flag));
    memset(map1,0,sizeof(map1));
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m ; j++)
            scanf("%c",&map1[i][j]);
        getchar();
    }
  /*  for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m ; j++){
            printf("%c",map1[i][j]);
        }
        printf("\n");
    }
    */
    printf("%d\n",bfs());
   }
return 0;
}

并查集写法:

#include<cstdio>
#include<cstring>
const int MAX = 2800;
int p[MAX];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int n,m;
char  map1[55][55];
int find(int x){
    return x == p[x] ? x : p[x] = find(p[x]);
}
int solve()
{
    int temp = 0;
    for(int i = 1 ;i <= n; i++)
        for(int j = 1; j <= m; j++)
           if(map1[i][j] == ‘#‘)
               temp ++;
   if(temp <= 2) return -1;
  for(int i = 1; i <= n ;i++){
     for(int j = 1; j <= m ;j++){
        if(map1[i][j] == ‘#‘){
          for(int k = 1; k <= n*m ;k++) p[k] = k;
           map1[i][j] = ‘.‘;
           for(int k = 1; k <= n; k++){
             for(int l = 1; l <= m; l++){
                if(map1[k][l] == ‘#‘){
                   for(int d = 0; d < 4 ; d++){
                      int x = k + dir[d][0];
                      int y = l + dir[d][1];
                      if( x >= 1 && x <= n && y >= 1 && y <= n && map1[x][y] == ‘#‘)
                          p[find(k*m+l)] = find(x*m+y);
                   }
                }
             }
          }
        int num = 0;
        for(int k = 1; k <= n*m;k++){
            if(find(k) == k)
                num++;
        }
        if(num > 1) return 1;
        map1[i][j] = ‘#‘;
        }
     }
  }
     return 2;
 }
int main()
{
        while(~scanf("%d%d",&n,&m)){
        memset(map1,0,sizeof(map1));
        memset(p,0,sizeof(p));
        getchar();
        for(int i = 1; i <= n ; i++){
            for(int j = 1;  j <= m; j++){
                scanf("%c",&map1[i][j]);
            }
            getchar();
        }
      /*  for(int i = 1; i <= n ; i++)
            for(int j = 1; j <= m;j++)
                printf("%c",map1[i][j]);
                */
        printf("%d\n",solve());
    }
    return 0;
}

时间: 2024-08-06 20:06:58

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)G——BFS——Cutting Figure的相关文章

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)D——数学——Triangles

Description 已知一个圆的圆周被  N 个点分成了 N 段等长圆弧,求任意取三个点,组成锐角三角形的个数. Input 多组数据. 每组数据一个N (N ≤  1000000). Output 对于每组数据,输出不同锐角三角形的个数. Sample Input 3 4 5 Sample Output 1 0 5大意:数学推导,分成奇数点偶数点讨论偶数时:只要两个相减就是答案奇数时同理:还有1ll*涨姿势用来变成long long 形式 #include<cstdio> #includ

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)F——GCD+1ll——LCM Challenge

Description Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it. But I also don't want to use many numbers, so I'll choose three positive integers (they do

ACdream区域赛指导赛之手速赛系列(5) 题解

A - Problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description The decimal numeral system is composed of ten digits, which we represent as "0123456789" (the digits in a system are

ACdream区域赛指导赛之手速赛系列(7)

A - Dragon Maze Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus 题目连接:     传送门 Problem Description You are the prince of Dragon Kingdom and your kingdom is in danger of running out of power. You must find

ACdream区域赛指导赛之手速赛系列(6)

Problem Description Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each column, each row, and all 9 non-overlapping 3x3 sub-matrices contain all of the digits from 1 through 9. Each 9x9 matrix is par

快速切题 acdream手速赛(6)A-C

Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each colu

Acdream手速赛7

蛋疼啊,本次只做出了一道题目...渣爆了... 妈蛋,,卡题之夜..比赛结果是1道题,比赛完哗啦哗啦出4道题.. A acdream1191 Dragon Maze 题意: 给一个迷宫,给出入口坐标和出口坐标,要求从入口到出口的步数尽可能少,如果有多种方案,则要求获得的分数尽可能多,获得的分数为经过的方格的数字之和 思路: bfs求最小步数,每走一步更新一下走到这个格子的最大权值 #include <bits/stdc++.h> using namespace std; typedef lon

Java制作最难练手速游戏,Faker都坚持不了一分钟

想练手速,来啊,互相伤害啊 Java制作最难练手速游戏,目测Faker也坚持不了一分钟 制作思路:只靠Java实现.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java.Java. 字母模型应该是整个游戏的主角,因为整个游戏过程中都涉及到它的运动,比如坠落,消失,产生等,首先应该考虑字母随即出现的位置,在游戏中不断下落,计算下落的高

猫和老鼠 蓝桥杯/手速/暴力练习赛(暴力搜索)

猫和老鼠 蓝桥杯/手速/暴力练习赛 [题目描述] 猫和老鼠在10*10 的方格中运动,例如: *...*..... ......*... ...*...*.. .......... ...*.C.... *.....*... ...*...... ..M......* ...*.*.... .*.*...... C=猫(CAT) M=老鼠(MOUSE) *=障碍物 .=空地 猫和老鼠每秒中走一格,如果在某一秒末他们在同一格中,我们称他们“相遇”. 注意,“对穿”是不算相遇的.猫和老鼠的移动方式相