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 namespace std;
const int maxn = 110;
char pic[maxn][maxn];
int vis[maxn][maxn];
int high,width;
int cnt = 0;
void dfs(int x,int y)
{
    //第x行,第y列
    if(pic[x][y] == ‘.‘ || vis[x][y]) return;
    if(x<0 || y<0 || x>=high || y>=width) return;
    vis[x][y] = 1;
    dfs(x-1,y);dfs(x+1,y);dfs(x,y+1);dfs(x,y-1);
    dfs(x-1,y-1);dfs(x-1,y+1);dfs(x+1,y-1);dfs(x+1,y+1);
}
int main()
{
    scanf("%d%d",&high,&width);
    for(int i = 0 ; i < high ; i ++) scanf("%s",pic[i]);
    for(int i = 0 ; i < high ; i ++) {
        for(int j = 0 ; j < width ; j ++) {
            if(pic[i][j] == ‘.‘) continue;
            if(vis[i][j] == 0) {
                dfs(i,j);
                cnt ++;
            }
        }
    }
    printf("%d\n",cnt);
    return 0;
}
时间: 2024-11-02 19:02:13

POJ2386 Lake Counting 图DFS的相关文章

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

poj2386 Lake Counting(简单DFS)

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

POJ2386 Lake Counting

这个题与UVa572 Oil Deposits完全相同,程序改两个字符,改了一下结束条件就通过了. 问题链接:POJ2386 Lake Counting. 题意简述:给定m×n矩阵 (1 <= N <= 100; 1 <= M <= 100),其中'W'代表水域,'.'代表陆地,问有几片湖. 本题可以使用深度优先搜索求解,用广度优先搜索也可以求解,差别不大. 这个程序说明如下: 1.方向数组 使用方向数组后,各个方向的试探的程序就会变得简洁了,用循环处理即可. 2.避免重复搜索 将

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

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

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

POJ2386 Lake Counting(DFS)

从任意的W开始,不停地把邻接的部分用'.'代替.1次DFS后与初始的这个W连接的所有W就都被替换成了'.',因此直到图中不再存在W为止,总共进行DFS的次数就是答案了.8个方向共对应了8种状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8×N×M)=O(N×M). #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace

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

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

poj-2386 lake counting(搜索题)

Time limit1000 ms Memory limit65536 kB 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')