• 细胞(搜索)


    问题 T: 【搜索】细胞

    时间限制: 1 Sec  内存限制: 64 MB
    提交: 7  解决: 7
    [提交] [状态] [讨论版] [命题人:外部导入]

    题目描述

    一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。

    输入

    用空格隔开的整数m,n(m行,n列)矩阵(1≤m,n≤100)。

    输出

    细胞的个数。

    样例输入

    4 10
    0234500067
    1034560500
    2045600671
    0000000089
    

    样例输出

    4
    逐个bfs,符合条件标记为0
    #include<bits/stdc++.h>
    #include<queue>
    #include<iostream>
    using namespace std;
    int p[105][105];
    struct node {
    	int x, y;
    };
    queue<node>q;
    int ans, d[4][2] = { {0,1},{0,-1},{1,0},{-1,0} },m,n;
    void bfs() {
    	while (!q.empty()) {
    		node tmp = q.front();
    		q.pop();
    		int cx = tmp.x, cy = tmp.y;
    		for (int i = 0; i < 4; i++) {
    			int ex = cx + d[i][0], ey = cy + d[i][1];
    			if (ex >= 1 && ey >= 1 && ex <= m && ey <= n && p[ex][ey] != 0) {
    				q.push(node{ ex,ey });
    				p[ex][ey] = 0;
    			}
    		}
    	}
    }
    int main() {
    	cin >> m >> n;
    	for (int i = 1; i <= m; i++) {
    		for (int j = 1; j <= n; j++) {
    			scanf("%1d",&p[i][j]);
    		}
    	}
    	for (int i = 1; i <= m; i++) {
    		for (int j = 1; j <= n; j++) {
    			if (!p[i][j])continue;
    			q.push(node{ i,j });
    			ans++;
    			p[i][j] = 0;
    			bfs();
    		}
    	}
    	cout << ans << endl;
    }
    

      

  • 相关阅读:
    friend ---- public and private
    c++中const使用详解
    In c++ access control works on per-class basis not on per-object basis.
    realloc 用法
    enum don't allocate any memory
    (转)C++ STL中的vector的内存分配与释放
    计算机网络面试总结
    Redis安装与测试
    Hbase的安装与测试
    使用ActiveMQ实现简易聊天功能
  • 原文地址:https://www.cnblogs.com/czy-power/p/10356461.html
Copyright © 2020-2023  润新知