zoukankan      html  css  js  c++  java
  • codeforces 616C The Labyrinth(flood fill)

    C. The Labyrinth

     

    You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.

    Let's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component.

    For each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y). You should do it for each impassable cell independently.

    The answer should be printed as a matrix with n rows and m columns. The j-th symbol of the i-th row should be "." if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit —- the answer modulo 10. The matrix should be printed without any spaces.

    To make your output faster it is recommended to build the output as an array of n strings having length m and print it as a sequence of lines. It will be much faster than writing character-by-character.

    As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printfinstead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

    Input

    The first line contains two integers n, m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the field.

    Each of the next n lines contains m symbols: "." for empty cells, "*" for impassable cells.

    Output

    Print the answer as a matrix as described above. See the examples to precise the format of the output.

    Sample test(s)
    input
    3 3
    *.*
    .*.
    *.*
    output
    3.3
    .5.
    3.3
    input
    4 5
    **..*
    ..***
    .*.*.
    *.*.*
    output
    46..3
    ..732
    .6.4.
    5.4.3
    Note

    In first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner).

    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<iterator>
    #include<queue>
    #include<set>
    #include<vector>
    #include<iostream>
    #include<map>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int INF=0x3f3f3f3f;
    char G[1005][1005];
    int mark[1005][1005];
    int cnt[1005*1005];
    int d[4][2]={1,0,0,1,-1,0,0,-1};
    int n,m;
    set<int>S;
    void dfs(int x,int y,int id)//dfs标记连通块
    {
        mark[x][y]=id;
        cnt[id]++;
        for(int i=0;i<4;i++)
        {
            int xx=x+d[i][0];
            int yy=y+d[i][1];
            if(!mark[xx][yy]&&G[xx][yy]!='*'&&xx>=0&&xx<n&&yy>=0&&yy<m)
            dfs(xx,yy,id);
        }
    }
    int main()
    {
        int id=0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",G[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(G[i][j]=='.'&&!mark[i][j])
                    dfs(i,j,++id);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                if(G[i][j]=='*')
                {
                    int ans=0;
                    for(int k=0;k<4;k++)
                    {
                        int x=i+d[k][0];
                        int y=j+d[k][1];
                        S.insert(mark[x][y]);
                    }
                    for(set<int>::iterator iter=S.begin();iter!=S.end();iter++)
                        ans+=cnt[*iter];//合并'*'四个方向的连通块,set去重。
                    S.clear();
                    ans++,ans%=10;
                    printf("%d",ans);
                }
                else
                    printf(".");
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    用BAT使用FTP命令上传文件
    BAT自动复制最新插件至运行程序
    requests模块源码阅读总结
    Lucene查询语法汇总
    Ansible scp Python脚本
    4.2 rust 命令行参数
    4.1 python中调用rust程序
    冒泡排序
    Golang开发命令行工具之flag包的使用
    MySQL基于Binlog的数据恢复实战
  • 原文地址:https://www.cnblogs.com/homura/p/5126685.html
Copyright © 2011-2022 走看看