zoukankan      html  css  js  c++  java
  • 计算细胞数【BFS】

    问题描述

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

    输入格式

    2行:第1行为两个整数 mm, nn, 代表矩阵的行数和列数。

    接下来 mm 行,每行有nn 个由0—9构成的字符。

    输出格式

    1个整数,表示细胞的个数。

    样例一

    input

    4 10
    0234500067
    1034560500
    2045600671
    0000000089
    

    output

    4
    

    数据范围与约定

    时间限制: 1s1s

    内存限制:256mb

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<cstdio>
     5 #include<string>
     6 #include<queue>
     7 #include<algorithm>
     8 #include<vector>
     9 using namespace std;
    10 int n,m,ans=0;
    11 char a[101][101];
    12 void bfs(int x,int y){
    13     if(x>n||x<1||y>m||y<1||a[x][y]=='0') return ;
    14     a[x][y]='0';
    15     bfs(x+1,y);
    16     bfs(x-1,y);
    17     bfs(x,y+1);
    18     bfs(x,y-1);
    19 }
    20 int main(){
    21     cin>>n>>m;
    22     for(int i=1;i<=n;i++){
    23         for(int j=1;j<=m;j++){
    24             cin>>a[i][j];
    25         }
    26     }
    27     for(int i=1;i<=n;i++){
    28         for(int j=1;j<=m;j++){
    29             if(a[i][j]!='0'){
    30                 ans++;
    31                 bfs(i,j);
    32             }
    33         }
    34     }
    35     cout<<ans<<endl;
    36     return 0;
    37 } 

    第一次发BFS

     

    256MB

  • 相关阅读:
    Valid Palindrome
    Path Sum II
    Reverse Integer
    Palindrome Number
    ZigZag Conversion
    函数调用堆栈 涉及汇编(转)
    关于字符指针和字符数组初始化的问题
    Remove Element(第一种方法参考别人)
    c内存分配(转)
    int *ptr=(int *)(&a+1)问题的探讨
  • 原文地址:https://www.cnblogs.com/luv-letters/p/8463291.html
Copyright © 2011-2022 走看看