zoukankan      html  css  js  c++  java
  • 1329:【例8.2】细胞

    http://ybt.ssoier.cn:8088/problem_show.php?pid=1329

    方法一:DFS

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n, m;
     4 char c;
     5 char a[1000][1000];
     6 int nex[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
     7 int ans;
     8 void testpr(){
     9     cout<<"----------"<<endl;
    10     for(int i=0; i<n; i++){    //输出测试 
    11         for(int j=0; j<m; j++)
    12             cout<<a[i][j];
    13         cout<<endl;
    14     }
    15 }
    16 void dfs(int x, int y){
    17     //testpr();//用于测试访问过程 
    18     for(int i=0; i<4; i++){
    19         int nx=x+nex[i][0];
    20         int ny=y+nex[i][1];
    21         //if(nx<0 || nx>n-1 || ny<0 || ny>m-1)continue;//判断是否越界 
    22         if(nx>=0 && nx<n && ny>=0 && ny<m && a[nx][ny]!='0')//判断是否为细胞 
    23         {
    24             a[nx][ny]='0';
    25             dfs(nx, ny);    
    26         }
    27     }
    28 }
    29 int main()
    30 {
    31     cin>>n>>m;
    32     getchar();     //过滤m后面的换行符 
    33     for(int i=0; i<n; i++){
    34         for(int j=0; j<m; j++){
    35             cin>>a[i][j];
    36         }
    37         getchar(); //过滤每行后面的换行符 
    38     }
    39     //testpr();//测试输入有无问题 
    40     for(int i=0; i<n; i++){    //找非0的点 
    41         for(int j=0; j<m; j++){
    42             if(a[i][j]!='0'){
    43                 ans++;
    44                 dfs(i, j);//从非0点开始搜索,搜索所到点都更改为0 
    45             }        
    46         }
    47     }
    48     cout<<ans;
    49     return 0;
    50  }

    方法二:BFS

     1 #include<bits/stdc++.h> 
     2 using namespace std;
     3 int n, m, ans;
     4 char a[60][60];
     5 struct node{     //结构体定义位置坐标 
     6     int x, y;
     7 };
     8 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//二维数组表示上下左右四个方向 
     9 node que[3600];//结构体数组存储队列 
    10 int f, r;//队首、队尾 
    11 void bfs(int x, int y){
    12     f=r=1;//初始化 
    13     que[r].x=x, que[r].y=y, a[x][y]='0';//非0数字坐标入队 
    14     while(f<=r){
    15         node t;                            //定义临时变量t来存放队首信息 
    16         t.x=que[f].x, t.y=que[f].y;
    17         for(int i=0; i<4; i++){            //四个方向遍历 
    18             int nx=t.x+dir[i][0];
    19             int ny=t.y+dir[i][1];
    20             if(nx>=0 && nx<n && ny>=0 && ny<m && a[nx][ny]!='0'){//判断是否符合入队条件 
    21                 a[nx][ny]='0';
    22                 r++;            //队尾后移准备入队 
    23                 que[r].x=nx;    //入队 
    24                 que[r].y=ny;    //入队 
    25             }
    26         }
    27         f++;//队首出队列 
    28     }
    29 }
    30 int main()
    31 {
    32     cin>>n>>m;
    33     for(int i=0; i<n; i++)cin>>a[i];
    34     for(int i=0; i<n; i++){
    35         for(int j=0; j<m; j++){
    36             if(a[i][j]!='0'){
    37                 ans++;
    38                 bfs(i, j);
    39             }
    40         }
    41     }
    42     cout<<ans;
    43     return 0;
    44  } 
  • 相关阅读:
    POJ 3468 线段树 成段更新 懒惰标记
    hdu 1717
    3个技巧让你正能量满满
    this指针基础介绍
    数组指针和指针数组的区别
    for循环的执行顺序
    读取文本文件里的数字求平均值
    break与continue的区别
    谈谈 静坐
    医生告诉我们的常识.读完它吧,你会一生受益
  • 原文地址:https://www.cnblogs.com/tflsnoi/p/13737183.html
Copyright © 2011-2022 走看看