Lake Counting (POJ No.2386)

有一个大小为N*M的园子,雨后积起了水,八连通的积水被认为是链接在一起的求出园子里一共有多少水洼?

***

*W*

***

/**
*进行深度优先搜索,从第一个W开始,将八个方向可以到达的 W修改为 .
*每次进行深度优先搜索的时候就将链接的水坑换成了.
*进行的深度优先搜索的次数就是水坑数
*/
#include<stdio.h>
#include<string.h>
const int  MAX=100;
int N,M;
char filed[MAX][MAX];  //园子的构造
//现在的位置(x,y)
void dfs(int x,int y){
    //将现在所在的位置替换为‘.’
    filed[x][y]=‘.‘;
    //循环遍历移动的8个方向
    for(int dx=-1;dx<=1;dx++){
        for(int dy=-1;dy<=1;dy++){
            int nx=x+dx,ny=y+dy;
            //判断(nx,ny是不是在园子内,以及是否有积水)
            if(0<=nx && nx<N && 0<=ny && ny<M && filed[nx][ny]==‘W‘)
            dfs(nx,ny);
        }
    }
    return;
}
void solve(){
    int res=0;
    for(int i=0;i<N;i++)
        for(int j=0;j<M;j++){
            if(filed[i][j]==‘W‘){
                dfs(i,j);      //从有积水的地方开始遍历,遍历的次数就是水坑的个数
                res++;
            }
        }
    printf("%d\n",res);
}
int main(){
    while(scanf("%d%d",&N,&M)==2){
        getchar();
        for(int i=0;i<N;i++)
            for(int j=0;j<M;j++)
                scanf("%c",&filed[i][j]);
        solve();
    }
    return 0;
} 
时间: 2024-10-11 12:29:13

Lake Counting (POJ No.2386)的相关文章

Lake Counting --POJ 2386

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 l

DFS----Lake Counting (poj 2386)

Lake Counting(POJ No.2386) 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')

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: 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

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解法)

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 (dfs+染色)

-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= 100; 1 <= M <= 100) 的正方形来表示.农场中的每个格子可以用'W'或者是'.'来分别代表积水或者土地,约翰想知道他的农场中有多少池塘.池塘的定义:一片相互连通的积水.任何一个正方形格子被认为和与它相邻的8个格子相连. 给你约翰农场的航拍图,确定有多少池塘 Input Line 1

poj2386 Lake Counting(简单DFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1562 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢