POJ 2386 Lake Counting(bfs解法)

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 <= 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.

Input

* 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.

Output

* Line 1: The number of ponds in Farmer John‘s field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

Source

USACO 2004 November

#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

#define ll long long
int n, m, sum;;
char position[100+8][100+8], mov[8][2] = {0,1, 1,0, 0,-1, -1,0, 1,1, 1,-1, -1,-1, -1,1};

struct coordinate
{
    int x, y;
};

void bfs(int a, int b)
{
    queue<coordinate>q;
    coordinate miao;
    miao.x = a;
    miao.y = b;
    q.push(miao);
    while(!q.empty())
    {
        coordinate fir;
        fir = q.front();
        coordinate next;
        q.pop();//在这里就要弹出,不然后面会插入很多东西
        for(int i = 0; i<8; i++)
        {
            next.x = fir.x+mov[i][0];
            next.y = fir.y+mov[i][1];
            if(next.x >= 0 && next.x<n && next.y >= 0 && next.y<m && position[next.x][next.y] == ‘W‘)
            {
                position[next.x][next.y] = ‘.‘;
                q.push(next);
            }
        }
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    getchar();
    sum = 0;
    for(int i = 0; i<n; i++)
    {
        for(int j = 0; j<m; j++)
        {
            scanf("%c", &position[i][j]);
        }
        getchar();
    }
    for(int  i = 0; i<n; i++)
        for(int j = 0; j<m; j++)
            if(position[i][j] == ‘W‘)
            {
                position[i][j] = ‘.‘;
                bfs(i, j);
                sum++;
            }
    printf("%d\n", sum);
    return 0;
}

原文地址:https://www.cnblogs.com/RootVount/p/10452986.html

时间: 2024-08-02 05:40:36

POJ 2386 Lake Counting(bfs解法)的相关文章

poj 2386 Lake Counting(BFS解法)

Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45142   Accepted: 22306 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 搜索题解

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

《挑战》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

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

POJ 2386 Lake Counting【BFS】

题意:给出一个矩形,问有多少块连通的W 当找到W的时候,进行广搜,然后将搜过的W变成点,直到不能再搜,进行下一次广搜,最后搜的次数即为水塘的个数 看的PPT里面讲的是种子填充法. 种子填充算法: 从多边形区域的一个内点开始,由内向外用给定的颜色画点直到边界为止.如果边界是以一种颜色指定的,则种子填充算法可逐个像素地处理直到遇到边界颜色为止 对于这一题: 先枚举矩阵中的每一个元素,当元素为W的时候,对它进行种子填充(BFS) 种子填充过程: 1)将八个方向的状态分别加进队列 2)如果元素为W,将其

POJ 2386 - Lake Counting 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2386 题目大意: 给出一张N*M的地图(1<=N,M<=100),地图上的W表示水坑,.表示陆地.水坑是八方向连通的,问图中一共有多少个水坑. Sample Input 9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0 Sample Output 6 5 分析: 非常简单的深搜.两重循环遍历这个地图,遇到W就dfs来求出这一整个大水坑,并把搜索过

poj - 2386 Lake Counting &amp;&amp; hdoj -1241Oil Deposits (简单dfs)

http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). 1 #include <cstdio> 2 3 char filed[110][110]; 4 int n,m; 5 void dfs(int x,int y) 6 { 7 for(int i=-1;i<=1;i++) 8 for(int j=-1;j<=1;j++) //循环遍历8

POJ 2386——Lake Counting

链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace std; const int MAX_M=105,MAX_N=105; char a[MAX_N][MAX_M]; int N,M; //现在位置 (x,y) void dfs(int x,int y){ a[x][y]='.'; //将现在所在位置替换为'.',即旱地 for(int dx=-1;dx<=

POJ 2386 Lake Counting (水题,DFS)

题意:给定一个n*m的矩阵,让你判断有多少个连通块. 析:用DFS搜一下即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring&