• 水池数目 ----- 图中有几个 单块的区域


    水池数目
    时间限制: 3000ms内存限制: 128000KB 64位整型:      Java 类名:
    上一题  提交  运行结果  统计   讨论版  下一题
    题目描述
    南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池。
    输入
    第一行输入一个整数N,表示共有N组测试数据
    每一组数据都是先输入该地图的行数m(0<m<100)与列数n(0<n<100),然后,输入接下来的m行每行输入n个数,表示此处有水还是没水(1表示此处是水池,0表示此处是地面)
    输出
    输出该地图中水池的个数。
    要注意,每个水池的旁边(上下左右四个位置)如果还是水池的话的话,它们可以看做是同一个水池。
    样例输入
    2
    3 4
    1 0 0 0 
    0 0 1 1
    1 1 1 0
    5 5
    1 1 1 1 0
    0 0 1 0 1
    0 0 0 0 0
    1 1 1 0 0
    0 0 1 1 1
    样例输出
    2
    3

    t挺简单的 一道题目  下面附上深搜代码

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 #include<set>
     9 #include<stack>
    10 #include<string>
    11 #include<sstream>
    12 #include<map>
    13 #include<cctype>
    14 using namespace std;
    15 int n,m,b[4][2]={0,-1,0,1,-1,0,1,0},a[105][105];
    16 void DFS(int y,int x)
    17 {
    18     a[y][x]=0;
    19     for(int i=0;i<4;i++)
    20     {
    21         int ex=x+b[i][0],ey=y+b[i][1];
    22         if(ex>=0&&ex<m&&ey>=0&&ey<n&&a[ey][ex]==1)
    23             DFS(ey,ex);
    24     }
    25 }
    26 int main()
    27 {
    28     int t,count1=0;
    29     scanf("%d",&t);
    30     while(t--)
    31     {
    32         scanf("%d%d",&n,&m);
    33         for(int i=0;i<n;i++)
    34             for(int j=0;j<m;j++)
    35             scanf("%d",&a[i][j]);
    36         for(int i=0;i<n;i++)
    37             for(int j=0;j<m;j++)
    38         {
    39             if(a[i][j]==1)
    40             {
    41                 DFS(i,j);
    42                 count1++;
    43             }
    44         }
    45         printf("%d
    ",count1);
    46         count1=0;
    47     }
    48 }

     广搜

     1 #include<stdio.h>
     2 #include<queue>
     3 using namespace std;
     4 int N,n,m,count;
     5 int b[4][2]={0,-1,0,1,-1,0,1,0},a[101][101];
     6 struct node
     7 {
     8     int x,y;
     9 };
    10 queue<node>Q;
    11 void bfs(int x,int y)             //  广搜 借助队列实现  // 深搜  借助 栈实现
    12 {
    13     int i,j;
    14     node q;
    15     q.x=x;
    16     q.y=y;
    17     Q.push(q);   //  将 水坑 压进 队列
    18     while(!Q.empty())   //   队列 不为 空
    19     {
    20         node e,w;
    21         e=Q.front();
    22         a[e.x][e.y]=0;     //  填  这一个 水坑
    23         for(i=0;i<4;i++)
    24         {
    25             w.x=e.x+b[i][0];
    26             w.y=e.y+b[i][1];
    27             if(w.x>=0&&w.x<=m&&w.y>=0&&w.y<=n&&a[w.x][w.y])         //    如果是水坑的话
    28             {
    29                 a[w.x][w.y]=0;
    30                 Q.push(w);
    31             }
    32         }
    33         Q.pop();           //  将已经用掉的 抛出去
    34     }
    35 }
    36 int main()
    37 {
    38     int i,j;
    39     scanf("%d",&N);
    40     while(N--)
    41     {
    42         scanf("%d%d",&m,&n);
    43         for(i=0;i<m;i++)
    44             for(j=0;j<n;j++)
    45             scanf("%d",&a[i][j]);
    46         for(count=i=0;i<m;i++)
    47             for(j=0;j<n;j++)
    48             {
    49                 if(a[i][j]==1)
    50                 {
    51                     count++;
    52                     bfs(i,j);
    53                 }
    54             }
    55             printf("%d
    ",count);
    56     }
    57 }
  • 相关阅读:
    读javascript高级程序设计08-引用类型之Global、Math、String
    读javascript高级程序设计07-引用类型、Object、Array
    读javascript高级程序设计06-面向对象之继承
    读javascript高级程序设计05-面向对象之创建对象
    读javascript高级程序设计04-canvas
    读javascript高级程序设计03-函数表达式、闭包、私有变量
    读javascript高级程序设计02-变量作用域
    C#将Word转换成PDF方法总结(基于Office和WPS两种方案)
    【转】 C#中Finally的一个不太常见的用法
    一看就懂的ReactJs入门教程-精华版
  • 原文地址:https://www.cnblogs.com/A-FM/p/5247557.html
Copyright © 2020-2023  润新知