首页 > 其他 > 详细

POJ 2386 Lake Counting

时间:2018-05-12 10:42:42      阅读:140      评论:0      收藏:0      [点我收藏+]

扫全图
遇到没标记的水开始DFS,并++Ans
对八个方向坐标增量打表以减少码量

#include <iostream>
#include <string>

using namespace std;

const int MAXN=111;
const int MAXM=111;

string input;
int N, M;
int isW[MAXN][MAXM];
bool Vis[MAXN][MAXM];
int dx[8]={-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8]={1, 0, -1, -1, 1, 0, -1, 1};
int Ans=0;

bool In(int x, int y){
    return (x>=1 && x<=N && y>=1 && y<=M);
}

void DFS(int x, int y){
    Vis[x][y]=true;
    for(int i=0, xx, yy;i<8;++i){
        xx=x+dx[i];yy=y+dy[i];
        if(In(xx, yy) && !Vis[xx][yy] && isW[xx][yy])
            DFS(xx, yy);
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    
    cin >> N >> M;
    for(int i=1;i<=N;++i){
        cin >> input;
        for(int j=1;j<=M;++j){
            isW[i][j]=(input[j-1]==‘W‘);
        }
    }
    
    for(int i=1;i<=N;++i){
        for(int j=1;j<=M;++j){
            if(!Vis[i][j] && isW[i][j]){
                DFS(i, j);
                ++Ans;
            }
        }
    }
    
    cout << Ans << endl;
    
    return 0;
}

/*
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

3

*/

POJ 2386 Lake Counting

原文:https://www.cnblogs.com/Pickupwin/p/9027761.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!