poj 2386  Lake Counting DFS

n*m的矩阵
W是水  .是地
问有多少池塘
池塘的定义是:
W通过 八个 方向连接成的一片算作是一个池塘

样例输入中,有左上、左下、右侧三片连接在一起的W
因而样例输出给出了3个池塘的答案

#include <cstdio>
#include <iostream>
#define N 110
using namespace std;
int n,m;
char s[N][N];
void dfs(int x,int y)
{
    s[x][y]=‘.‘;
    int i,j;
    for(i=-1; i<=1; i++)
        for(j=-1; j<=1; j++)
        {
            int nx=x+i,ny=y+j;
            if(nx<0||nx>=n||ny<0||ny>=m||s[nx][ny]==‘.‘)
                continue;
            dfs(nx,ny);
        }
}
int main()
{
    scanf("%d%d%*c",&n,&m);
    int ans=0,j,i;
    for(i=0; i<n; i++)
        cin>>s[i];
    for(i=0; i<n; i++)
        for(j=0; j<m; j++)
            if(s[i][j]==‘W‘) {
                dfs(i,j);
                ans++;
            }
    cout<<ans<<endl;
    return 0;
}
时间: 2024-08-14 21:01:59

poj 2386  Lake Counting DFS的相关文章

【POJ - 2386】Lake Counting (dfs+染色)

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

【poj 2386】Lake Counting

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

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

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(DFS)

题意:有一个大小为N×M的园子,雨后积起了水.八连通的积水被认为是连在一起的.求园子里一共有多少水洼? * * * * W*    (八连通指的就是左图中相对W的*的部分) * * * 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 O

POJ2386 Lake Counting 图DFS

找出有多少个"水洼"(W的联通块) 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 解题思路 DFS 代码 #include <cstdio> #include <cstring> #include <algorithm> using namesp

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

POJ 2386 Lake Counting 搜索题解

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