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;
    }
    

      

  • 相关阅读:
    剑指OFFER之复杂链表的复制(九度OJ1524)
    剑指OFFER之二叉树中和为某一值的路径(九度OJ1368)
    剑指OFFER之从二叉搜索树的后序遍历序列(九度OJ1367)
    剑指OFFER之从上往下打印二叉树(九度OJ1523)
    剑指OFFER之栈的压入、弹出序列(九度OJ1366)
    剑指OFFER之包含min函数的栈(九度OJ1522)
    剑指OFFER之顺时针打印矩阵(九度OJ1391)
    剑指OFFER之树的子结构(九度OJ1520)
    剑指OFFER之二叉树的镜像(九度OJ1521)
    VM安装CentOs7虚拟机后无法上网之解决方法
  • 原文地址:https://www.cnblogs.com/csushl/p/9386618.html
Copyright © 2011-2022 走看看