CSU-ACM2017暑期训练6-bfs I

来源:互联网 发布:《电力网络》 编辑:程序博客网 时间:2024/05/16 15:24

题目:

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

  


            题目大意:给你一个图里面有'*'和‘.’问你每个*的上下左右方向所联通的‘.’的个数.然后输出的时候用数字代表*(数字对10取余).

           思路:一开始理所当然想到直接对每个*进行bfs,当然是超时啦,正确的做法是先用bfs或dfs对‘.’的连通块进行预处理,也就是对不同的联通快编号并且计数。然后直接对'*'的上下左右看有哪些联通快即可,这里注意上下左右也有可能出现相同的联通快,所以记得判重。

          代码:


#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 1000000000;const int maxn = 100;int T,n,m,k=1;char maps[1111][1111];long long book[1111][1111];long long sums[1111][1111];long long num[11111111];int nexts[4][2]={0,1,-1,0,1,0,0,-1};void dfs(int a,int b){    book[a][b]=k;    num[k]++;    for(int i=0;i<4;i++)    {        int aa=a+nexts[i][0];        int bb=b+nexts[i][1];        if(aa>=0&&bb>=0&&aa<n&&bb<m&&book[aa][bb]==0&&maps[aa][bb]=='.')            dfs(aa,bb);    }    return;}int main(){    scanf("%d%d ",&n,&m);    memset(book,0,sizeof(book));    memset(num,0,sizeof(num));    for(int i=0;i<n;i++)        scanf("%s",maps[i]);    for(int i=0;i<n;i++)        for(int j=0;j<m;j++)            if(maps[i][j]=='.'&&book[i][j]==0)            {                dfs(i,j);                k++;            }    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            int sum=0;            if(maps[i][j]=='*')            {                set<int>t;                for(int p=0;p<4;p++)                {                    int a=i+nexts[p][0];                    int b=j+nexts[p][1];                    if(maps[a][b]=='.'&&a>=0&&b>=0&&a<n&&b<m&&!t.count(book[a][b]))                    {                        t.insert(book[a][b]);                        sum+=(num[book[a][b]]%10);                    }                }                sums[i][j]=sum;            }        }    }    for(int i=0;i<n;i++)    {        for(int j=0;j<m;j++)        {            if(maps[i][j]=='.')                printf(".");            else            {                printf("%d",(sums[i][j]+1)%10);            }        }        printf("\n");    }    return 0;}


原创粉丝点击