zoukankan      html  css  js  c++  java
  • Coderfocers-616c

    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 nstrings 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/printf instead 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.

    Example

    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).

    题意:这个题如果用普通的DFS的话对每个*遍历会TE。所以我们换个思路,不如先把每个.所在区域的大小DFS出来。然后再去遍历每个*,将其相连的.区域加起来(注意有的. 在同一区域,要用到map和set容器)

    AC代码为:

    #include<cstdio>  
    #include<cstring>  
    #include<iostream>  
    #include<set>  
    #include<map>  
    #include<algorithm>
    using namespace std;
    
    int n, m;
    char e[1005][1005];
    int dir[4][2] = { 1,0,0,1,-1,0,0,-1};
    int vis[1005][1005];
    int cnt, num;
    set<int> s;
    map<int, int> mp;
    
    void dfs(int x, int y)
    {
    int i, j;
    for (i = 0; i < 4; i++)
    {
    int tx = x + dir[i][0];
    int ty = y + dir[i][1];
    if (tx <= 0 || tx > n || ty <= 0 || ty > m || vis[tx][ty] || e[tx][ty] == '*')
    continue;
    cnt++;
    vis[tx][ty]=num;
    dfs(tx, ty);
    }
    }
    
    
    int main()
    {
    int i, j;
    num=0;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++)
    scanf("%s",e[i]+1);
    
    for (i=1;i<=n;i++)
    for (j=1;j<=m;j++)
    {
    if (!vis[i][j] && e[i][j] == '.')
    {
    num++;
    cnt = 1;
    vis[i][j]=num;
    dfs(i, j);
    mp[num] = cnt;
    }
    }
    for (i = 1; i <= n; i++) 
    {
    for (j = 1; j <= m; j++)
    {
    if (e[i][j] == '*') 
    {
    int cc = 0;
    s.clear();
    
    for (int k = 0; k < 4; k++)
    {
    int tx = i + dir[k][0];
    int ty = j + dir[k][1];
    if (e[tx][ty] == '.' && !s.count(vis[tx][ty]))
    {
    s.insert(vis[tx][ty]);
    cc += mp[vis[tx][ty]];
    }
    }
    printf("%d", (cc + 1) % 10);
    }
    else
    printf("%c", e[i][j]);
    }
    printf("
    ");
    }
    return 0;
    }
    

      

  • 相关阅读:
    [UE4]使用C++重写蓝图,SpawnObject根据类型动态创建UObject
    [UE4]在C++中使用中文变量和中文注释
    [UE4]The global shader cache file missing 运行错误解决办法
    [UE4]自定义结构体、类、数据表
    [UE4]使用UFUNCTION注意事项
    [UE4]一个好用的虚幻4插件,根据资源名称动态加载资源,GetCurrentLeveName(获得当前地图名称)
    [UE4]小地图UI放在哪里创建合适?
    [UE4]动态改变相机OrthWidh、关掉阴影
    [UE4]迁移小地图到其他工程
    [UE4]正交
  • 原文地址:https://www.cnblogs.com/csushl/p/9386618.html
Copyright © 2011-2022 走看看