zoukankan      html  css  js  c++  java
  • code vs 3492 细胞个数

    题目链接:http://codevs.cn/problem/3492/

    https://www.luogu.com.cn/problem/P1451

    题目描述 Description

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

    0234500067
    1034560500
    2045600671
    0000000089
    有4个细胞。

    输入描述 Input Description

    【输入格式】

    整数m,n

    (m行,n列)矩阵

    输出描述 Output Description

    【输出格式】

    细胞的个数。

    样例输入 Sample Input

    4  10

    0234500067
    1034560500
    2045600671
    0000000089

    样例输出 Sample Output

    4

    数据范围及提示 Data Size & Hint

    1<=m,n<=1000

    题目分析:

    首先,题目描述的数据格式跟后台检测的数据格式不一致。后台检测的数据格式类似于下面:

    4 10
    0 2 3 4 5 0 0 0 6 7
    1 0 3 4 5 6 0 5 0 0
    2 0 4 5 6 0 0 6 7 1
    0 0 0 0 0 0 0 0 8 9

    所以,输入数据时应该要用int数组直接保存即可,不是用char数组保存。

    【算法思路】
    ⑴从文件中读入m*n矩阵阵列,将其转换为bool矩阵存入b数组中;
    ⑵沿b数组矩阵从上到下,从左到右,找到遇到的第一个细胞;
    ⑶将细胞的位置入队h,入队后的位置b数组置为flase;
    ⑷将h队的队头出队,沿其上、下、左、右四个方向上的细胞位置入队,入队后的位置b数组置为flase;
    ⑸重复4,直至h队空为止,则此时找出了一个细胞;
    ⑹重复2,直至矩阵找不到细胞;
    ⑺输出找到的细胞数。


    AC代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 bool b[101][101];
     7 int n,m,a[101][101],ans=0;
     8 int dx[4] = {-1,0,1,0},
     9     dy[4] = {0,1,0,-1};
    10 void bfs(int x, int y)
    11 {
    12     int hx[1000],hy[1000],head=1,tail=1,tx,ty;
    13     ans++;
    14     hx[1]=x,hy[1]=y;
    15     b[x][y]=false;
    16     for(;head<=tail;++head)
    17     {
    18         for (int i=0;i<=3;++i)
    19         {
    20             tx=hx[head]+dx[i],
    21             ty=hy[head]+dy[i];
    22             if(tx>0 && tx<=m && ty>0 && ty<=n && b[tx][ty])
    23             {
    24                 tail++;
    25                 hx[tail]=tx,
    26                 hy[tail]=ty;
    27                 b[tx][ty]=false;
    28             }
    29         }
    30     }
    31 }
    32 int main()
    33 {
    34     scanf("%d%d",&m,&n);
    35     for (int i=1;i<=m;++i)
    36         for (int j=1;j<=n;++j)
    37             b[i][j]=true;
    38     for (int i=1;i<=m;++i)
    39         for (int j=1;j<=n;++j)
    40         {
    41             scanf("%1d",&a[i][j]);
    42             if (!a[i][j]) b[i][j]=false;
    43         }
    44     for (int i=1;i<=m;++i)
    45         for (int j=1;j<=n;++j)
    46             if(b[i][j]) bfs(i,j);
    47     printf("%d",ans);
    48     return 0;
    49 }
    View Code

    另一种写法:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<queue>
     4 using namespace std;
     5 int main(int argc, char *argv[])
     6 {
     7     queue<int> qx,qy;
     8     int n,m,i,j,x,y;
     9     int a[1002][1002];
    10     int count=0;//细胞个数 
    11     
    12     scanf("%d%d",&n,&m);
    13     for(i=0;i<n;i++)
    14     {
    15         for(j=0;j<m;j++)
    16         {
    17             scanf("%d",&a[i][j]);
    18             //printf("%d ",a[i][j]);
    19         }
    20         //printf("
    ");
    21     }
    22 
    23     for(i=0;i<n;i++)
    24     {
    25         for(j=0;j<m;j++)
    26         {
    27             if(a[i][j]!=0)
    28             {
    29                 qx.push(i);   qy.push(j);
    30                 a[i][j]=0;
    31                 while(!qx.empty())
    32                 {
    33                     x=qx.front();  qx.pop();
    34                     y=qy.front();  qy.pop();
    35 
    36                     if(y+1<m&&a[x][y+1]!=0)//当前坐标x,y的右边 
    37                     {
    38                         qx.push(x);  qy.push(y+1);
    39                         a[x][y+1]=0;
    40                     }
    41                     if(x+1<n&&a[x+1][y]!=0)//当前坐标x,y的下边
    42                     {
    43                         qx.push(x+1);  qy.push(y);
    44                         a[x+1][y]=0;
    45                     }
    46                     if(y-1>=0&&a[x][y-1]!=0)//当前坐标x,y的左边
    47                     {
    48                         qx.push(x); qy.push(y-1);
    49                         a[x][y-1]=0;
    50                     }
    51                     if(x-1>=0&&a[x-1][y]!=0)//当前坐标x,y的上边
    52                     {
    53                         qx.push(x-1); qy.push(y);
    54                         a[x-1][y]=0;
    55                     }
    56                 }
    57                 count++;
    58             }
    59         }
    60     }
    61     printf("%d
    ",count);/**/
    62     return 0;
    63 }
  • 相关阅读:
    可复用的自定义Adapter
    SharedPreference工具类
    MD5工具类
    面试题
    策略模式
    java画图之初体验
    接口与事件之图形界面的认证登录
    “奥特曼“与”小怪兽”的继承者们之战
    “奥特曼攻打小怪兽”java学习打怪升级第一步
    使用智能指针管理对象资源
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/7206318.html
Copyright © 2011-2022 走看看