【ACM-POJ】poj2386,Lake Counting

2 篇文章 0 订阅
#include <iostream>
#include <stdio.h>
//#define DBG
using namespace std;
const int MAXSIZE = 10000;
char map[MAXSIZE][MAXSIZE];
int N,M;
unsigned long LakeCounts;
bool iFirstLake = true;
bool iCounted = true;
void dfs(int x,int y)
{
    int m,n;
    n = x;m = y;
    //边界条件,防止越界
    if(m >= M || m < 0) return;
    if(n >= N || n < 0) return;
    if(map[n][m] == 'W')
    {
     //   if(!iFirstLake)
    //    {
            if(!iCounted)
            {
                LakeCounts++;
#ifdef DBG
                cout << "LAKE COUNTS ADD!";
                cout << endl;
                cout << "AT LINE : " << n;
                cout << " column : " << m << endl;
#endif
                iCounted = true;
            }
     //   }
        /*
        if(iFirstLake)
        {
            iFirstLake = false;
            iCounted = false;
        } */
#ifdef DBG
        cout << "This is line " << n << " column " << m;
        cout << " and this is " << map[n][m] << endl;
#endif
        map[n][m] = '.';

        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()
{
    char buf;
     freopen("b:\\acm\\poj2386\\input.txt","r",stdin);
    cin >> N >> M;
    for(int i = 0 ; i < N ; i++)
        for(int j = 0 ; j < M ; j++)
        {
            cin >> buf;
            if (buf != '\0')
                map[i][j] = buf;
        }

///这里从0开始搜索貌似不行
#ifdef DBG
    for(int i = 0;i < N ; i++)
    {
        for(int j = 0; j < M ; j++)
        {
            cout << map[i][j];
        }
        cout << endl;
    }
#endif
    for(int i=0;i<N;i++)
    {
        for(int j = 0; j < M;)
        {
            if(map[i][j] != 'W')
            {
                j++;continue;
            }
            //map[i][j] == 'W'
            /*
            iCounted = true;
            iFirstLake = true; */
            iCounted = false;
            dfs(i,j);
            j++;
        }
    }
    cout << LakeCounts << endl;
  //  fclose(stdin);
    //cout << "Hello world!" << endl;
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值