zoukankan      html  css  js  c++  java
  • AcWing池塘计数

    这个题让我们求连通块得数数量,我考虑用flood fill算法。

    也就是枚举这个地图每一个点,假如符合要求就bfs与这个点联通的点,并打上标记。结束后接着枚举没有被标记并且符号要求的点。。。

    1.==和=千万别写错

    2.队列不要忘记head++;

    3.tail++一遍就可以了

    代码

    #include<bits/stdc++.h>
    #define maxn 1010 
    using namespace std;
    char mp[maxn][maxn];
    int n,m;
    struct node{
        int x,y;
    }q[maxn*maxn];
    int ans=0;
    bool book[maxn][maxn];
    void bfs(int sx,int sy){
        int head=1,tail=0;
        q[++tail].x=sx;
        q[tail].y=sy;
        book[sx][sy]=true;
        while(head<=tail){    
            for(int i=q[head].x-1;i<=q[head].x+1;i++){
                for(int j=q[head].y-1;j<=q[head].y+1;j++){
                    if(i==q[head].x&&j==q[head].y) continue;
                    if(i<1||i>n||j<1||j>m) continue;
                    if(mp[i][j]=='.'||book[i][j]==true) continue;
                    if(book[i][j]==false&&mp[i][j]=='W'){
                        q[++tail].x=i;
                        q[tail].y=j;
                        book[i][j]=true;
                    }
                }
            }
            head++;
        }
    }
    int cnt=0;
    int main(){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mp[i][j];
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(mp[i][j]=='W'&&book[i][j]==false){
                    cnt++;
                    bfs(i,j);
                }
            }
        }
        cout<<cnt;
        return 0;
    }
  • 相关阅读:
    goreplay~基本知识
    goreplay~http输出队列
    goreplay~拦截器设置
    goreplay~流量变速
    goreplay~http过滤参数
    goreplay~文件输出解析
    goreplay~http输出工作线程
    Antlr4 语法解析器(下)
    2021最新版Eclipse的下载和使用
    MySQL中drop、truncate和delete的区别
  • 原文地址:https://www.cnblogs.com/china-mjr/p/11802529.html
Copyright © 2011-2022 走看看