zoukankan      html  css  js  c++  java
  • NYOJ--27--dfs--水池数目

    /*
        Name: NYOJ--27--水池数目
        Author: shen_渊 
        Date: 17/04/17 15:42
        Description: 经典dfs水题,,, 
    */
    #include<iostream>
    #include<cstring>
    using namespace std;
    int m,n,map[105][105],ct;
    void dfs(int,int);
    int dir[8][2] = {
                        {0,1},
                        {0,-1},
                        {1,0},
                        {-1,0},
                    };//4个方向递归 
    int main()
    {
        ios::sync_with_stdio(false);
    //    freopen("in.txt","r",stdin);
        int N;cin>>N;
        while(N--)
        {
            cin>>m>>n;
            ct = 0;
            memset(map,0,sizeof(map));
            for(int i=0; i<m; ++i){
                for(int j=0; j<n; ++j){
                    cin>>map[i][j];
                }
            }
            for(int i=0; i<m; ++i){
                for(int j=0; j<n; ++j){
                    if(map[i][j] == 1){
                        map[i][j] = 0;
                        ct++;
                        dfs(i,j); 
                    }
                }
            }
            cout<<ct<<endl;
        }
        return 0;
    }
    void dfs(int x,int y){
        for(int i=0; i<8; ++i){
            int a = x + dir[i][0];
            int b = y + dir[i][1];
            if(a<0 || b<0 || a>=m || b>=n || map[a][b] == 0)continue;
            map[a][b] = 0;
            dfs(a,b);
        }
    }
  • 相关阅读:
    密码学浅析
    FireWall Mark + LVS
    tcp/ip首部
    iptables(二)网络防火墙
    BIND服务
    LVS(一)
    QQ、微信消息轰炸
    LVS四种模型(二)
    安装和克隆
    压缩和打包
  • 原文地址:https://www.cnblogs.com/langyao/p/7251875.html
Copyright © 2011-2022 走看看