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\n",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 }
时间: 2024-11-04 16:44:33

1388:Lake Counting的相关文章

Openjudge1388 Lake Counting【DFS/Flood Fill】

http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   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 <= 1

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

poj 2386 Lake Counting

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24578   Accepted: 12407 Description 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 <= 10

【POJ2386】Lake Counting

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22861   Accepted: 11530 Description 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 <= 10

1751: [Usaco2005 qua]Lake Counting

1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 150[Submit][Status][Discuss] Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle

3385: [Usaco2004 Nov]Lake Counting 数池塘

3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 21[Submit][Status][Discuss] Description 农夫约翰的农场可以表示成N×M(1≤N,M≤100)个方格组成的矩形.由于近日的降雨, 在约翰农场上的不同地方形成了池塘.每一个方格或者有积水(’W’)或者没有积水(’.’).农夫约翰打算数出他的农场上共形成了多少池塘.一个

POJ2386 Lake Counting 【DFS】

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20782   Accepted: 10473 Description 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 <= 10

POJ 之2386 Lake Counting

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20003   Accepted: 10063 Description 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 <= 10

《挑战》2.1 POJ 2386 Lake Counting (简单的dfs)

1 # include<cstdio> 2 # include<iostream> 3 4 using namespace std; 5 6 # define MAX 123 7 8 char grid[MAX][MAX]; 9 int nxt[8][2] = { {1,0},{0,-1},{-1,0},{0,1},{1,1},{1,-1},{-1,1},{-1,-1} }; 10 int n,m; 11 12 int can_move( int x,int y ) 13 { 14