zoukankan      html  css  js  c++  java
  • CF598D DFS+连通块

    容易发现同一连通块的答案相等 ,故可以一次遍历图中empty点的答案并保存,保存方法可以建立一个数组,下表是对应的连通块序号

    #include<iostream>
    #include<string>
    #include<cmath>
    #include<cstring>
    #include<vector>
    #include<map>
    #include<set>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<sstream>
    #include<cstdio>
    #define INF 0x3f3f3f3f
    #define read(i) scanf("%d",&i)
    const int maxn = 1e6 + 5;
    const double PI = acos(-1.0);
    typedef long long ll;
    using namespace std;
    
    
    int tot;
    int cnt=1;    
    int n, m;
    int vis[1005][1005];
    char mp[1005][1005];
    int res[1000000 + 5];
    int dx[] = { -1,0,1,0 };
    int dy[] = { 0,1,0,-1 };
    
    int in(int x, int y) {
        return x > 0 && x <= n && y > 0 && y <= m;
    }
    
    void dfs(int x, int y) {
        vis[x][y] = cnt;
        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i], yy = y + dy[i];
            if (in(xx, yy) && !vis[xx][yy]) {
                //printf("%d %d", xx, yy);
                if (mp[xx][yy] == '.')   dfs(xx, yy);
                else if (mp[xx][yy] == '*')  tot++;
            }
        }
    }
    
    int main() {
        int k;
        read(n), read(m), read(k);
        for (int i = 1; i <= n; i++) {
            scanf("%s", mp[i] + 1);
        }
        int x, y;
    
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (mp[i][j] == '.' && !vis[i][j]) {
                    tot = 0;
                    dfs(i, j);
                    res[++cnt] = tot;
                }
            }
        }
    
        while (k--) {
            read(x), read(y);
            printf("%d\n",res[vis[x][y]+1]);
        }
        return 0;
    }
  • 相关阅读:
    Shodan新手入坑指南
    linux 下查看网卡工作速率
    centos关闭ipv6
    springBoot----@ConditionalOnxxx相关注解总结
    ElasticSearch Root身份运行
    CentOS6 Install kafka
    CentOS 7 中firewall-cmd命令
    sensu
    metrics+spring+influxdb
    SpringBoot(十二):SpringBoot整合Kafka
  • 原文地址:https://www.cnblogs.com/hznumqf/p/12450923.html
Copyright © 2011-2022 走看看