P2156 [SDOI2009]细胞探索

$ \color{#0066ff}{ 题目描述 }$

生物课上,老师开始为同学们介绍细胞。为了加深同学们的印象,老师在一张N×M的矩阵中定义了一种细胞,矩阵中仅有井号“#”和点“.”:

细胞由细胞核、细胞质及细胞膜构成。细胞核是一个4连通(上下左右相连)的全为“#”的连通块,它必须实心,即不能存在一个4连通的“.”连通块被其完全包围(所谓完全包围指的是,这个“.”连通块不能位于矩阵边界相邻,且它的4相邻格子均属于包含它的“#”连通块)。细胞膜是一个8连通(上下左右,以及4个对角方向)的全为“#”的非实心连通块。细胞膜仅包围一个4连通的区域,且这个区域内有且仅有一个细胞核,这个区域剩下的位置全为“.”。

所有连通块必须极大化,即一个8连通块周围不能找到一个“#”与这个连通块的任意一个“#”8连通;同样,对于一个4连通块周围不能找到一个“#”与这个连通块的任意一个“#”4连通。

现在,老师画了一幅图画,并让小E回答图画中一共有几个细胞,并把图画中不属于任何一个细胞的“#”改成“.”。

\(\color{#0066ff}{输入格式}\)

输入文件explore.in的第一行包含两个用空格分隔的正整数N和M,表示矩阵的高和长。

接下来一个N行M列的矩阵,矩阵中仅含井号“#”和点“.”,保证没有多余字符。

\(\color{#0066ff}{输出格式}\)

输出文件explore.out第一行包含一个整数,表示输入的矩阵中的细胞数。

接下来一个N行M列的矩阵,矩阵中仅含井号“#”和点“.”,表示更改后的图画。

\(\color{#0066ff}{输入样例}\)

12 13
.###..#####..
#...#.#....#.
#.#.#.#..#.#.
#...#..#...#.
.###.#..###..
....#..##...#
..........###
##########..#
#...........#
#.###...###.#
#...........#
#############


9 14
#########.....
#.......#....#
#.#####.#...#.
#.#...#.#..#..
#.#.#.#.#.#..#
#.#...#.#..#..
#.#####.#...#.
#.......#....#
#########.....


7 15
#######.#######
#.....#.#.....#
#.###.#.#.###.#
#.#.#.#.#.#...#
#.###.#.#.###.#
#.....#.#.....#
#######.#######

\(\color{#0066ff}{输出样例}\)

1
......#####..
......#....#.
......#..#.#.
.......#...#.
........###..
.......##....
.............
.............
.............
.............
.............
.............

    
1
..............
..............
..#####.......
..#...#.......
..#.#.#.......
..#...#.......
..#####.......
..............
..............

    
1
........#######
........#.....#
........#.###.#
........#.#...#
........#.###.#
........#.....#
........#######

\(\color{#0066ff}{数据范围与提示}\)

对于20%的数据,满足1 ≤ N, M ≤ 20。

另有20%的数据,满足所有“#”都属于某一个正确的细胞。

对于100%的数据,满足1 ≤ N, M ≤ 1,000。

\(\color{#0066ff}{题解}\)

看起来很难下手,如果搜井号的话,还要考虑内外,肯定很麻烦的
那么我们不如从点入手
不难发现,一个合法的细胞,细胞质连接且仅连接两个联通块
所以可以先预处理出所有井号联通块,然后。。。过程见代码。。
#include<bits/stdc++.h>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 1050;
const int inf = 0x7fffffff;
int bel[maxn][maxn], n, m, mp[maxn][maxn], num, siz[maxn * maxn], cnt[maxn * maxn];
int up[maxn * maxn], bj[maxn * maxn], ls, fa[maxn * maxn], must[maxn * maxn];
std::pair<int, int> spe[maxn * maxn];
char sss[maxn];
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int rx[] = {1, -1, 0, 0};
int ry[] = {0, 0, 1, -1};
bool vis[maxn][maxn], flag, have;
std::set<int> v;
void dfs(int x, int y, int id) {
    siz[id]++;
    up[id] = std::min(up[id], x);
    bel[x][y] = id;
    vis[x][y] = true;
    for(int i = 0; i < 8; i++) {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy] && mp[xx][yy]) dfs(xx, yy, id);
    }
}
void expand(int x, int y) {
    if(x == 1 || x == n || y == 1 || y == m) flag = true;
    vis[x][y] = true;
    for(int i = 0; i < 4; i++) {
        int xx = x + rx[i];
        int yy = y + ry[i];
        if(xx >= 1 && yy >= 1 && xx <= n && yy <= m) {
            if(mp[xx][yy]) v.insert(bel[xx][yy]);
            if(!mp[xx][yy] && !vis[xx][yy]) expand(xx, yy);
        }
    }
}
void init(int x, int y) {
#ifdef olinr
    printf("init %d %d\n", x, y);
#endif
    ls++;
    vis[x][y] = true;
    for(int i = 0; i < 4; i++) {
        int xx = x + rx[i];
        int yy = y + ry[i];
        if(xx >= 1 && xx <= n && yy >= 1 && yy <= m) {
#ifdef olinr
            printf("in");
#endif
            if(!mp[xx][yy] && !vis[xx][yy]) have = true;
#ifdef olinr
            if(mp[xx][yy]) printf("!");
            if(!vis[xx][yy]) printf("$");
#endif
            if(mp[xx][yy] && !vis[xx][yy]) init(xx, yy);
        }
    }
}


void work(int x, int y) {
    v.clear();
    flag = false;
    have = false;
    ls = 0;
    expand(x, y);
    if(flag) {
#ifdef olinr
        //搜索到边界一定不是细胞质
    printf("break at 1\n");
#endif
        return;
    }
    if(v.size() != 2) {
        //一个膜里面一大堆不知道什么玩意
        int pos = 0, mx = inf;
        for(std::set<int>::iterator it = v.begin(); it != v.end(); it++)
            if(mx > up[*it]) mx = up[*it], pos = *it;
        //这个膜肯定没用了
        bj[pos] = true, cnt[pos] += v.size() - 1, must[pos] = true;
        for(std::set<int>::iterator it = v.begin(); it != v.end(); it++)
            if(*it != pos) fa[*it] = pos;
#ifdef olinr
        printf("vis: ");
        for(std::set<int>::iterator it = v.begin(); it != v.end(); it++) printf("%d ", *it);
        puts("");
        printf("break at 2\n");
#endif
        return;
    }
    std::set<int>::iterator it = v.begin();
    int aa = *it;
    it++;
    int bb = *it; 
    if(up[aa] > up[bb]) std::swap(aa, bb);
    if(bj[aa]) {
#ifdef olinr
        printf("break at 3\n");
#endif
        //已经用过了,就是两个粘在一起的细胞,然而在本题是不合法的
        must[aa] = true;
        return;
    }
    init(spe[bb].first, spe[bb].second);
    if(have) {
        //细胞核里有TM细胞质!!!我去
        must[aa] = true;
#ifdef olilnr
        printf("break at 4\n");
#endif
        return;
    }
    if(ls != siz[bb]) {
        //因为搜井号联通块是8联通的,这里要4联通搜一下,看是不是完整
#ifdef olinr
        printf("%d %d\n", ls, siz[bb]), printf("break at 5\n");
#endif
        must[aa] = true;
        return;
    }
    fa[aa] = 0, fa[bb] = aa;
    cnt[aa]++;
}
//must:是否一定不合法,bj是否曾经被作为细胞膜,fa,细胞核属于哪个细胞膜, cnt,作为细胞膜的次数
int main() {
    n = in(), m = in();
    for(int i = 1; i <= n; i++) {
        scanf("%s", sss);
        for(int j = 1; j <= m; j++)
            mp[i][j] = sss[j - 1] == '#';
    }
    //处理井号联通块
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(mp[i][j] && !vis[i][j]) {
                num++;
                up[num] = inf;
                spe[num] = std::make_pair(i, j);
                dfs(i, j, num);
#ifdef olinr
                printf("id: %d, siz = %d\n", num, siz[num]);
#endif
            }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++) 
            vis[i][j] = 0;
    //对于每个点的联通块,处理
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(!mp[i][j] && !vis[i][j]) {
#ifdef olinr
                printf("now dfs %d %d\n", i, j);
#endif
                work(i, j);
            }
    int ans = 0;
    for(int i = 1; i <= num; i++) {
        bj[i] = false;
        if(!must[i] && !fa[i] && cnt[i] == 1) ans++, bj[i] = true;
    }
    printf("%d\n", ans);
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(!mp[i][j]) putchar('.');
            else {
                if(bj[bel[i][j]] || bj[fa[bel[i][j]]]) putchar('#');
                else putchar('.');
            }
        }
        puts("");
    }
    return 0;
}

转载于:https://www.cnblogs.com/olinr/p/10445464.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值