• 1388:Lake Counting


    题目链接:

    NOI题库http://noi.openjudge.cn/ch0205/1388/

    POJ 2386 http://poj.org/problem?id=2386 

    总时间限制: 1000ms  内存限制: 65536kB
    描述
    Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

    Given a diagram of Farmer John's field, determine how many ponds he has.
    输入
    * Line 1: Two space-separated integers: N and M

    * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
    输出
    * Line 1: The number of ponds in Farmer John's field.
    样例输入
    10 12
    W........WW.
    .WWW.....WWW
    ....WW...WW.
    .........WW.
    .........W..
    ..W......W..
    .W.W.....WW.
    W.W.W.....W.
    .W.W......W.
    ..W.......W.
    样例输出
    3
    提示
    OUTPUT DETAILS:
    There are three ponds: one in the upper left, one in the lower left,and one along the right side.

    题目大意:

    有一块N*M的土地,雨后机起了水,有水标记为'W',干燥标记为'.'。
    八连通的积水被认为是连接在一起的。需要求出院子里有多少水洼。
    首先输入N和M,然后输入N*M的二维字符数组,行内字符之间没有空格。
    输出水洼的数目。
    N和M都在100以内。

    算法分析:
    这道题可以用深搜,也可以用广搜。

    扫描二维数组,每遇到一个'W'就从这个地方出发开始深搜或广搜。
    搜索过程中,把搜缩遇到的'W'全部设为'.'。
    每当完成一次深搜或广搜,水洼数增1.


    题解可以参考:
    http://blog.csdn.net/c20180630/article/details/52329915
    http://www.cnblogs.com/sooner/archive/2013/04/09/3010938.html

    深搜的思路:

    下面是我自己写的代码,含深搜与广搜的代码:

      1 #include<stdio.h>
      2 #include<iostream>
      3 #include<queue>
      4 #include<stack>
      5 using namespace std;
      6 
      7 struct obj
      8 {
      9     int xx,yy;
     10 };
     11 
     12 int N,M;
     13 char a[102][102];
     14 int Count;
     15 
     16 int dx[8]={-1,-1,0,1,1,1,0,-1};//从正上方开始,顺时针旋转的8个方向 
     17 int dy[8]={0,1,1,1,0,-1,-1,-1};
     18 void BFS(int x,int y);//从(x,y)开始广搜 
     19 void DFS(int x,int y);//从(x,y)开始深搜 
     20 void DFS2(int x,int y);//从(x,y)开始深搜,递归实现 
     21 
     22 int main(int argc, char *argv[])
     23 {
     24     freopen("1388.in","r",stdin);
     25     int i,j;
     26     scanf("%d%d",&N,&M);getchar();//吸收回车符 
     27     for(i=0;i<N;i++)
     28     {
     29         gets(a[i]);
     30         /*
     31         for(j=0;j<M;j++)
     32         {
     33             a[i][j]=getchar();
     34         }
     35         getchar();//吸收回车符
     36         */
     37     }
     38     
     39     Count=0;
     40     for(i=0;i<N;i++)
     41     {
     42         for(j=0;j<M;j++)
     43         {
     44             if(a[i][j]=='W')
     45             {
     46                 BFS(i,j);//从(i,j)开始广搜 
     47                 //DFS(i,j);//从(i,j)开始深搜 
     48                 //{ DFS2(i,j); Count=Count+1;} //递归实现的深搜,从(i,j)开始深搜 
     49             }
     50         }
     51     }
     52     printf("%d
    ",Count);
     53     return 0;
     54 }
     55 void BFS(int x,int y)//从(x,y)开始广搜
     56 {
     57     queue<struct obj> q;
     58     struct obj start,temp;
     59     int i,txx,tyy;
     60 
     61     start.xx=x;
     62     start.yy=y;
     63     q.push(start);
     64     a[x][y]='.';
     65     while(!q.empty())
     66     {
     67         for(i=0;i<8;i++)
     68         {
     69             txx=q.front().xx+dx[i];
     70             tyy=q.front().yy+dy[i];
     71             if(txx>=0&&txx<N&&tyy>=0&&tyy<M&&a[txx][tyy]=='W')
     72             {
     73                 temp.xx=txx;
     74                 temp.yy=tyy;
     75                 a[txx][tyy]='.';
     76                 q.push(temp);
     77             }
     78         }
     79         q.pop();
     80     }
     81     Count++;
     82 }
     83 
     84 void DFS(int x,int y)//从(x,y)开始深搜 
     85 {
     86     stack<struct obj> s;
     87     struct obj start,temp,temp2;
     88     int i,txx,tyy;
     89     
     90     a[x][y]='.';
     91     start.xx=x;
     92     start.yy=y;
     93     s.push(start);
     94     while(!s.empty())
     95     {
     96         temp=s.top();  s.pop();
     97         for(i=0;i<8;i++)
     98         {
     99             txx=temp.xx+dx[i];
    100             tyy=temp.yy+dy[i];
    101             if(txx>=0&&txx<N&&tyy>=0&&tyy<M&&a[txx][tyy]=='W')
    102             {
    103                 temp2.xx=txx;
    104                 temp2.yy=tyy;
    105                 a[txx][tyy]='.';
    106                 s.push(temp2);
    107             }
    108         }
    109     }
    110     Count++;
    111 }
    112 
    113 void DFS2(int x,int y)//从(x,y)开始深搜,递归实现
    114 {
    115     int i,txx,tyy;
    116     a[x][y]='.';
    117     for(i=0;i<8;i++)
    118     {
    119         txx=x+dx[i];
    120         tyy=y+dy[i];
    121         if(txx>=0&&txx<N&&tyy>=0&&tyy<M&&a[txx][tyy]=='W')
    122         {
    123             //a[txx][tyy]='.';
    124             DFS2(txx,tyy);
    125         }
    126     }
    127 }
  • 相关阅读:
    【重构学习】12 重构学习感想
    【重构学习】11 大型重构
    【重构学习】10 继承关系的重构
    【重构学习】09 函数调用的重构
    【重构学习】08 条件表达式的重构
    嵊州D4T1 翻车 rollover 真的翻车了
    计算圆内格点数
    嵊州D3T2 福尔贝斯太太的快乐夏日 summer
    嵊州D3T3 light
    嵊州D3T1 山魔 烙饼问题
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/7258841.html
Copyright © 2020-2023  润新知