zoukankan      html  css  js  c++  java
  • 细胞(搜索)

    问题 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;
    }
    

      

  • 相关阅读:
    Httpclient请求数据(post)
    实现定位(无基本地图)
    Httpclient请求数据
    带参数路径的刷新和加载
    AsyncTask异步交互和httpurlconnection结合使用
    判断网络状态
    Android数字签名
    app加固
    定位和xml解析和gson解析加上拉加载,下拉刷新
    下载app后自动安装程序
  • 原文地址:https://www.cnblogs.com/czy-power/p/10356461.html
Copyright © 2011-2022 走看看