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;
}
View Code

 并查集写法:

#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;
}
View Code

 

posted @ 2015-04-17 19:48  Painting、时光  阅读(228)  评论(0编辑  收藏  举报