Poj 2386 Lake Counting

click here~~

                                                       ***Lake Counting***

Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 24201  Accepted: 12216 

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

解题思路:
dfs  ,注意从8个方面进行搜索:
上代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n,m,ans;
char map[110][110];

bool judge(int i,int j)
{
    if(i>=n || i<0)
        return false;
    if(j>=m || j<0)
        return false;
    if(map[i][j] == '.')
        return false;
    return true;
}

void dfs(int i,int j)
{
    map[i][j] = '.';
    if(judge(i+1, j))
        dfs(i+1, j);
    if(judge(i, j+1))
        dfs(i, j+1);
    if(judge(i-1, j))
        dfs(i-1, j);
    if(judge(i, j-1))
        dfs(i, j-1);
    if(judge(i+1, j+1))
        dfs(i+1, j+1);
    if(judge(i-1, j-1))
        dfs(i-1, j-1);
    if(judge(i+1, j-1))
        dfs(i+1, j-1);
    if(judge(i-1, j+1))
        dfs(i-1, j+1);
}

int main()
{
    while(cin>>n>>m)
    {
        getchar();
        ans=0;
        for(int i=0; i<n; i++)
            gets(map[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(map[i][j] == 'W')
                {
                    ans++;
                    dfs(i,j);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-12-21 22:50:22

Poj 2386 Lake Counting的相关文章

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj 题型分类

主流算法: 1.搜索 //回溯 2.DP(动态规划) 3.贪心 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同等安置矩形的并的面积与周长 7.组合数学 //Polya定理 8.模拟 9.数据结构 //并查集.堆 10.博弈论 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 2377, 2380, 1318, 1877, 1928, 1971,

hduoj题目分类

基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196.1197.1201.1202.1205.1219.1234.1235.1236.1248.1

ACM练级

一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上.  下面给个计划你练练: 第一阶段: 练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打 出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)  3.大数(

poj 1656 Counting Black

这个本来应该是不水的,估计题意原来是让用线段树或者树状数组做的,但是,题目里的数据规模太小了,直接模拟就过了..也就成超级水题了. #include <stdio.h> #include <string.h> int map[103][103]; //在外部定义自动初始化为0 int main() { int n; scanf("%d",&n); char input[10]; //黑.白.测试 int x,y,L; //边界界定值 int i,j; i

POJ 1077 Eight:八数码问题

题目链接: http://poj.org/problem?id=1077 题目类型: 隐式图搜索 原题: The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all pac

UVa 757 / POJ 1042 Gone Fishing: 枚举&amp;amp;贪心&amp;amp;想法题&amp;amp;优先队列

757 - Gone Fishing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698 http://poj.org/problem?id=1042 John is going on a fishing trip. He has h hours available ( ), and t

reference counting:PHP源码分析-变量的引用计数、写时复制(Reference counting &amp;amp; Copy-on-Write)

PHP语法中有两种赋值方式:引用赋值.非引用赋值.<?php$a = 1;$b = $a; // 非引用赋值$c = &$b; // 引用赋值从表面看,通常会这样认为:"引用赋值就是两个变量对应同一个变量(在C中其实就是一个zval),非引用赋值则是直接产生的一个新的变量(zval),同时将值copy过来".这种认为在大部分情况下都是可以想通的.(#1)但有些情况下则会显得非常低效,例如:(#2)<?phpfunction print_arr($arr){//非引用